0

Hello I can't figure out why a) this code errors on the last copy and b) in the debugger it shows picture[0] containing 0 to 4 then containing 1 to 4 (see picture1)

char picture[5][5];
strcpy(picture[0], "#####");
strcpy(picture[1], "#####");
strcpy(picture[2], "#####");
strcpy(picture[3], "#####");
strcpy(picture[4], "#####");

When xcode errors it shows the following

enter image description here

0

4 Answers 4

3

In C strings ends with the character \0. So you shall reserve space for them by declaring the array as:

char picture[5][6];
Sign up to request clarification or add additional context in comments.

Comments

2

When you declare char str[n] it means n-1 chars are reserved for you. While the last one is a '\0' char (terminator). If you overwrite the '\0' char, it goes on printing till it finds the '\0' in memory.

So modify char picture[5][5]; to char picture[5][6];

Comments

1

You forgot that you need a null byte to terminate the strings. (In fact, your last strcpy() writes outside the bounds of the array, invoking undefined behaviour.) Use:

char picture[5][6] = { "#####", "#####", "#####", "#####", "#####", };

This saves on the strcpy() calls too.


I commented:

The compiler might well generate warnings if you change the 6 to 5, but I think it is required to allow the initializers (even though it would have to drop the terminating null bytes). But it is a warning that you'd want to fix; it is showing a genuine problem.

In fact, GCC 4.8.2 doesn't even warn about the issue under fairly stringent compilation options.

Compilation:

gcc -O3 -g -std=c11 -Wall -Wextra -Werror ols.c -o ols

Code:

#include <stdio.h>

static char picture1[5][5] = { "#####", "#####", "#####", "#####", "#####", };
static char picture2[5][6] = { "#####", "#####", "#####", "#####", "#####", };

int main(void)
{
    for (int i = 0; i < 5; i++)
        printf("%s\n", picture1[i]);
    for (int i = 0; i < 5; i++)
        printf("%s\n", picture2[i]);
    return 0;
}

Output:

#########################
####################
###############
##########
#####
#####
#####
#####
#####
#####

3 Comments

Thanks. Thought I tried the syntax you gave before and it didn't work (though it might have been also a null byte problem).
The compiler might well generate warnings if you change the 6 to 5, but I think it is required to allow the initializers (even though it would have to drop the terminating null bytes). But it is a warning that you'd want to fix; it is showing a genuine problem.
Ya I fixed it though later on I really didn't use them as it was all in a for loop that would never go to the end. Thanks
0

Whenever you are using a function from strings.h, make sure you take care of "\0", otherwise you will always get unexpected output at the end of the string(esp. when printing with %s). The compiler wont give any warnings because it wouldn't find anything wrong. When you are printing with %s the compiler will look for "\0", and keep printing until then when you run the program. In fact when you are coping using strncpy, if have an array size 9 and you only use 5, the rest 4 will have null stored in them.

Comments

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.