0

Thanks in advance for any help. I'm making a simple program in C and I've already declared two strings called: "message1" & "message2", how would I go about changing the contents of these strings? I initially fill them with "empty" for the check that happens in the segment of code shown below:

char message1[32] = "empty";
…
if(message1 != "empty");
{
        printf("\n[USER 1]: %s", message1);
        message1 = "empty";
}

After this check, if message1 contains anything but the original value, it will then print said value and then reset message1 to its original value of "empty". However, this is obviously not the case. I've Googled for the answer and am very confused.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
wafwoof
  • 35
  • 7
  • Please include the declaration of `message1`. And why don't you think the code you've shown works? – Stephen Newell Sep 01 '20 at 04:05
  • what is `message1` defined as? I don't think they have a `string` type in C. Take a look at `strcmp` and `strcpy` on the google. – Andy Sep 01 '20 at 04:06
  • See [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237). Given `char message1[32] = "empty";`, you have an array that you can modify by many possible mechanisms, including `strcpy()` and friends. Direct assignment to individual characters also works; just ensure that the string is properly null terminated. – Jonathan Leffler Sep 01 '20 at 04:17
  • 3
    The comparison shown will always fail — you must use `strcmp()` or an equivalent function to compare strings in C. The assignment shown won't compile either — you cannot assign arrays like that and need to use `strcpy()` or an equivalent function. – Jonathan Leffler Sep 01 '20 at 04:21

2 Answers2

2

You seem to be confusing array semantics with pointer semantics.

If your string (which is a data format) is stored in an array, you can change individual characters. But you cannot assign to an array. So this line

        message1 = "empty";

becomes a constraint violation. Instead you should use strcpy(), or better snprintf() (don't be tempted to use strncpy() -- it is not a safer strcpy()). Also you cannot meaningfully compare the array and a string literal. So this line

if(message1 != "empty");

also doesn't make any sense. Use the strcmp() function. (Also the semicolon is probably not what you want because it terminates the statement so the following compound statement is not controlled by the if statement.

If you don't need to modify the individual characters, then you can just use a char * to point to the start of a string. Then the assignment is valid, and the comparison is probably ok (it relies upon the compiler consolidating string literals which is not required by the standard, but all the good compilers will do this).

luser droog
  • 18,988
  • 3
  • 53
  • 105
0

Two things needs to be swapped.

1.

if (message1 != "empty");

Use strcmp() to compare strings. The logical operators aren't used for comparison between strings in C.

if (strcmp(message1,"empty"))
{
    ....
}

2.

message1 = "empty";

You can't assign arrays in C by a string. Use strcpy for that.

strcpy(message1,"empty");