Room in the Array

The array of a string has a fixed size, like any array. char name[] = "Anna"; has exactly 5 elements: room for 4 letters and the '\0', nothing more. If you want to store a longer text in it later, give the array more room when you create it:

char nickname[20] = "Anna";
int size = sizeof(nickname);
int length = strlen(nickname);
printf("%d %d\n", size, length);

This prints:

20 4

The array has 20 elements. The text takes the first five elements, with its '\0', and the rest are zeros, just like with int numbers[5] = {0};. That's why sizeof and strlen now give different numbers: the size of the array, and the length of the text in it.

How do you put a new text into the array? Not with =. As you know from the lesson on arrays, an array can't be assigned all at once:

char nickname[20] = "Anna";
nickname = "Bob";
main.c:5:14: error: array type 'char[20]' is not assignable

Use the function strcpy (string copy) from string.h instead. It copies the characters of a text, including its '\0', into an array. In the parentheses, write first where to copy, then what:

char nickname[20] = "Anna";
strcpy(nickname, "Bob");
printf("%s\n", nickname);

This prints:

Bob

The order is the same as with =: the target is on the left. A shorter text is no problem. Its '\0' marks the new end, and whatever is left in the array after it doesn't matter anymore.

The array must have room for the whole text and its '\0'. strcpy doesn't check that: it doesn't know how big the array is, and it copies until it reaches the '\0'. If the text doesn't fit, it goes on writing past the end of the array, into memory that belongs to something else. When Clang can see that the text won't fit, the program doesn't compile here:

char nickname[5] = "Anna";
strcpy(nickname, "Alexander");
main.c:6:5: error: 'strcpy' will always overflow; destination buffer has size 5, but the source string has length 10 (including NUL byte) [-Werror,-Wfortify-source]

The message says that the target (destination buffer) has room for 5 characters, but the text (source string) needs 10, with the null character (NUL byte). Clang can't always see it, though, for example when the text comes from another array. Then nothing stops the program, and it goes on with damaged data. Mistakes like this have caused many security holes in real programs. So before every strcpy, check that the length of the text plus 1 fits into the array.

The same goes for the starting text. C allows char name[4] = "Anna";, but then there's no room left for the '\0', and the text has no end:

char city[] = "Paris";
char name[4] = "Anna";
printf("%s\n", name);
printf("%s\n", city);

This prints:

AnnaParis
Paris

The first line should be just Anna. But printf didn't find a '\0' after it, so it went on printing whatever was in memory after the array, which here happened to be the array city.

Instructions

The player starts a game. Below the printf, copy the text "playing a game" into status with strcpy, and print the status again with the same printf. Run the program: it doesn't compile, because status only has room for "online". Give the array room for 20 characters, char status[20] = "online";, and run it again. The output should end with the line Status: playing a game.

Then the player steps away for a moment. Copy "away" into status and print the status once more: Status: away.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

5/10