0

Why is it that the I seem not be able to use strcmp to compare two strings if I first create an array that just holds enough characters without the \0 character at the end and then put in the \0 at a later step like the following?

    // The following from strcmp returns non-zero, so the two strings buff, t1 are
    // not equal
    char buff[4] = "K7Ag";
    buff[4] = '\0';
    char t1[5] = "K7Ag";
    if(strcmp(buff, t1) == 0) {
        printf("they are equal\n");
    }

But if I do this:

// strcmp shows they are equal
    char buff[5] = "K7Ag";

    char t1[5] = "K7Ag";
    if(strcmp(buff, t1) == 0) {
        printf("they are equal\n");
    }

I know I need a null character at the end of each string for strcmp to work properly. So why is it that I first create buff as:

char buff[4] = "K7Ag"

then append "\0" like:
buff[4] = '\0'
then call
strcmp(buff, t1) would not work?

Finally, can I assume that when I create a buff like

char b1[100] = "Abcd"

then I will have 96 null characters (i.e., 96 of those \0) right after the character d in the b1 buffer?

So if I initialize a buffer with less than the allocated size, can I assume the rest of the buffer holds the \0 character?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hodondo
  • 83
  • 1
  • 5
  • 1
    `char buff[4]` means that `buff` is 4 characters long, so indexes 0 to 3 are valid. The initialiser fills those 4 characters but omits the nil-character, so `buff` should not be treated as a string. `buff[5]` is out of bounds and causes UB. – Cheatah Dec 01 '22 at 19:29
  • 1
    `buff[5]` is undefined behavior. – Mark Ransom Dec 01 '22 at 19:29
  • 1
    The place where you want the null byte is `buff[4]`, not `buff[5]`. But that too is out of bounds. – Nate Eldredge Dec 01 '22 at 19:30
  • 1
    You can't "attach" anything to the array. – 0___________ Dec 01 '22 at 19:31
  • @NateEldredge sorry I have fixed the index from buff[5] to buff[4] now, hope it is more clear. – hodondo Dec 01 '22 at 19:33
  • It's more clear, but it's still out of bounds. When you define `char buff[4]` then `buff` has four elements, namely `buff[0], buff[1], buff[2], buff[3]`. There is no `buff[4]` and when you attempt to write there, you overwrite unrelated memory. Arrays in C do not grow when you write past their end. – Nate Eldredge Dec 01 '22 at 19:34
  • @NateEldredge But can I assume if I initialize the array say : b3[10] = "AB", then I would have 8 null characters being attached at the end of B till the 10 position of the array? – hodondo Dec 01 '22 at 19:36
  • So if you really want to compare these strings with `strcmp`, then you will have to create a new larger buffer of size 5 or more, and copy the four elements of `buff` into the larger buffer, then write a null byte into the new buffer. There just isn't enough space in `buff`. – Nate Eldredge Dec 01 '22 at 19:36
  • I see you have edited your question and added more to it. If you are going to treat `buff` as a string, why do you care about the rest of the buffer being initialised to 0? You only need the string to be followed by one null-character. You don't need to care about the rest of the buffer. Yes, the rest would be zeroes, but does it really matter? – Cheatah Dec 01 '22 at 19:37
  • I would not use the word "attached". Your array `b3` has ten elements, and when you initialize only some of the elements, then the rest are initialized to zero (null bytes). So with `b3[10] = "AB"` then `b3[0]` contains `A`, `b3[1]` contains `B`, and `b3[2]..b3[9]` contain null bytes. – Nate Eldredge Dec 01 '22 at 19:38
  • @Cheatah the thing is I am using the read function to read something from a file to a buffer, however the number of bytes to read from could be variable. So I was just concerned when I call read to read something into the buffer, is it then safe that I can use strcmp to compare whatever I now have in the buffer with a string? i.e. whatever I now have in the buffer will have the null character following the last character that I read into the buffer? so I can use strcmp directly to compare the thing in buffer and the string without having to artificially write a null byte to the buffer? – hodondo Dec 01 '22 at 19:41
  • Use `strncmp` with the size returned by the read function. – Cheatah Dec 01 '22 at 19:48
  • @Cheatah oh, I will try that too. Thanks for your explanations. – hodondo Dec 01 '22 at 19:50
  • you just need `char buff[] = "K7Ag"`, that will be 5 bytes including a 0 for you – pm100 Dec 01 '22 at 22:37

0 Answers0