c-strings

How do I return a variable size string from a function?

一个人想着一个人 提交于 2019-12-13 20:15:52
问题 I need a working code for a function that will return a random string with a random length. What I want to do would be better described by the following code. char *getRandomString() { char word[random-length]; // ...instructions that will fill word with random characters. return word; } void main() { char *string = getRandomString(); printf("Random string is: %s\n", string); } For this, I am strictly forbidden to use any other include than stdio.h . Edit: This project will be adapted to be

strcat() crashes if using same array as both parameters

被刻印的时光 ゝ 提交于 2019-12-13 15:09:46
问题 char r[40]; strcpy(r,"abcdef"); strcat(r,r); My program crashes at the third line? Replacing strcat(r,r); by strcat(r,"abcdef"); works fine though.... why is that? 回答1: strcat() reads from the input and copies it to the output until it find a \0 terminator in the input. By specifying the same array for both input and output, you are modifying the input while it is being read from. You would have to check your compiler's particular implementation of strcat() , but if you trace through a simple

C string to uppercase in C and C++

你离开我真会死。 提交于 2019-12-13 12:53:01
问题 While I was putting together a to-uppercase function in C++ I noticed that I did not receive the expected output in C. C++ function #include <iostream> #include <cctype> #include <cstdio> void strupp(char* beg) { while (*beg++ = std::toupper(*beg)); } int main(int charc, char* argv[]) { char a[] = "foobar"; strupp(a); printf("%s\n", a); return 0; } Output as expected: FOOBAR C function #include <ctype.h> #include <stdio.h> #include <string.h> void strupp(char* beg) { while (*beg++ = toupper(

How to determine if strings are equal in Objective C?

♀尐吖头ヾ 提交于 2019-12-13 12:02:50
问题 I read a string from a JSON result as follows: NSString *strResult = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding]; I then try to determine if the string is equal to the value "N" if ([strResult isEqualToString:@"N"]) { [lblImageOK setHidden:YES]; } else { [lblImageOk setHidden:NO]; } The if statement allways returns the else part, even though the result is "N". They both have the same value but the statement returns false always. 回答1: I

No More Confusing Pointers

依然范特西╮ 提交于 2019-12-13 10:02:55
问题 Following is my code: #include <stdio.h> int main() { char a[10]="Hi!"; void f(char b[]) { // 1. printf("*a is:%s\n",*a); printf("&a is:%p\n",&a); // 2. printf("&&a is:%p\n",&(&a)); printf("a's address is:%p\n",a); printf("a's value is:%s\n",a); printf("b's address is:%p\n",b); printf("b's value is:%s\n",b); // 3. printf("*b is:%s\n",*b); printf("&b is:%s\n",&b); } f(a); return 1; getch(); } Running the above code gives the output: &a is:0028FF1C a's address is:0028FF1C a's value is:Hi! b's

Return Statement Returning a Null Pointer Value and Not the Desired Value

ⅰ亾dé卋堺 提交于 2019-12-13 09:00:01
问题 I ran this through debug, and in the String Substring function, everything works up until the return statement. 'returnString', in the code below, has the correct value when at the return line. However, as soon as I go to next line (the closing bracket directly after), it changes to: {Text=0x003ed0e0

Wrong types involving converting a vector to feed to execvp

心已入冬 提交于 2019-12-13 06:27:25
问题 I have a vector of strings vector<string> args that I need to convert to a c string to in turn run as arguments through an execvp call. I keep getting the error "invalid conversion from const char* to char* for the line in my loop. I don't understand how to fix this while still resulting in something I can feed to execvp. While I have seen similar posts, none of the solutions I have come across seem to fix my issue. int argsLen = c->args.size(); char **argv = new char* [c->args.size() + 1];

Is it Safe to strncpy Into a string That Doesn't Have Room for the Null Terminator?

拟墨画扇 提交于 2019-12-13 00:29:58
问题 Consider the following code: const char foo[] = "lorem ipsum"; // foo is an array of 12 characters const auto length = strlen(foo); // length is 11 string bar(length, '\0'); // bar was constructed with string(11, '\0') strncpy(data(bar), foo, length); cout << data(bar) << endl; My understanding is that string s are always allocated with a hidden null element. If this is the case then bar really allocates 12 characters, with the 12 th being a hidden '\0' and this is perfectly safe... If I'm

Why is fgets() and strncmp() not working in this C code for string comparison?

核能气质少年 提交于 2019-12-12 20:29:03
问题 This is a very fun problem I am running into. I did a lot of searching on stack overflow and found others had some similar problems. So I wrote my code accordingly. I originally had fscan() and strcmp() , but that completely bombed on me. So other posts suggested fgets() and strncmp() and using the length to compare them. I tried to debug what I was doing by printing out the size of my two strings. I thought, maybe they have /n floating in there or something and messing it up (another post

How to convert the time to a c string in c?

那年仲夏 提交于 2019-12-12 15:31:04
问题 I wanna to write something to a .txt file in .c file, but required to name that file with the current timestamp as the postfix, just like filename_2010_08_19_20_30. So I have to define the filename char array first and process the filename by myself?Assign the character one by one? Is there any easy way to do that? 回答1: There's a function called strftime that exists for the express purpose of writing a time value into a human-readable string. Documentation: http://linux.die.net/man/3/strftime