1

just started playing with C, I have this

char str_arr[2][3] = {"gou", "ram"};
printf("%s / %s / %s", str_arr, str_arr[0], str_arr[1]);

which prints:

gouram / gouram / ram

and

char str_arr[2][4] = {"gou", "ram"};
printf("%s / %s / %s", str_arr, str_arr[0], str_arr[1]);

prints:

gou / gou / ram

I really don't understand, the 4 is the maximum size, yet makes no sense at all.

//Edit Just wanted to say that this helped me a lot, it may be a dumb question for most of you, but for me it was not, I just got into memory allocation and more advanced stuff. Thank you SO!

13
  • It makes perfect sense once you know how strings & arrays work in C. What do you know about either of these? Commented Apr 18, 2022 at 15:46
  • 2
    Common undefined behaviour. Which book are you reading?
    – autistic
    Commented Apr 18, 2022 at 15:47
  • I'm watching CS50 on Youtube. Any book you would recommend to straighten it?
    – Martzy
    Commented Apr 18, 2022 at 16:06
  • @Martzy: In my opinion, the CS50 course does a very good job of explaining strings and the meaning of the null terminating character. Regarding books, you may want to take a look at this question. Commented Apr 18, 2022 at 16:07
  • 1
    @Martzy: If you learn from CS50, you should be aware that this course first tries to hide the true nature of strings from you, by using the typedef string instead of char *. Only in about week 4 of the course is the true nature of strings and pointers revealed to you. Overall, I have a very good impression of the course. Commented Apr 18, 2022 at 16:15

1 Answer 1

3

Passing str_arr to the function printf with the %s format specifier will invoke undefined behavior. The %s specifier requires a char * as an argument. The expression str_arr is not a char * and will also not decay into one. However, writing str_arr[0] instead of str_arr will decay to a char *.

In the first example

char str_arr[2][3] = {"gou", "ram"};

passing str_arr[0] will also invoke undefined behavior, for a different reason:

The %s format specifier as a function argument a pointer to a valid string, i.e. a pointer to a sequence of characters terminated by a null character. However, neither str_arr[0] nor str_arr[1] are terminated by a null character, because there is no room for one.

However, when you write

char str_arr[2][4] = {"gou", "ram"};

there is room for a terminating null character, and both str_arr[0] and str_arr[1] will have one after initialization, so the behavior of the programm is well-defined when passing these sub-arrays to the function printf (i.e. there is no undefined behavior).

10
  • Nice, so the problem is that the size was too small to have null at the end, which would concat them.
    – Martzy
    Commented Apr 18, 2022 at 16:01
  • 2
    @Martzy: In your case, the undefined behavior resulted in the strings being concatenated. However, you cannot rely on that. Undefined behavior is, by definition, unpredictable. Commented Apr 18, 2022 at 16:05
  • I understand now, yes, just in my case did that. Thank you.
    – Martzy
    Commented Apr 18, 2022 at 16:07
  • @AndreasWenzel: The strings are literally concatenated; arrays are defined to store elements contiguously, so putting characters in the separate elements of char str_arr[2][3] necessarily concatenates them in memory, and aliasing any object, including the entire array, through a char * is defined, and passing str_arr[0] passes such a char *. The only thing preventing printf("%s", str_arr[0]) from being defined to print “gouram” is the lack of a terminating null character. Had the array been defined char str_arr[3][3], there would be a terminating null character. Commented Apr 18, 2022 at 16:27
  • @EricPostpischil I think last time you wanted to say [2][4] instead of [3][3]
    – Martzy
    Commented Apr 18, 2022 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.