0
votes

I actually have a question regarding the concept of a char array, especially the one which is declared and initialized like below.

char aString[10] = "";

What i was taught was that this array can store up to 10 characters (index 0-9) and that at index 10 there is an automatically placed null terminating character (i know that accessing it would not be right) such that if we use string handling functions (printf, scanf, strcmp, etc.) they would know when the string stops.

However when I tried making a struct data type like below,

typedef struct customer{
    char accountNum[10];
    char name[100];
    char idNum[15];
    char address[200];
    char dateOfBirth[10];
    unsigned long long int balance;
    char dateOpening[10];
}CUSTOMER;

inserted 10 characters into accountNum (any method, e.g. scanf), and printf it, what is printed out will be accountNum and values in the first word of name (i know that printf will stop at a space or a '\0'). This indicates that a char array does not have a terminating null at the end of the array.

Does this mean that if we have a char array of size 10 (char aString[10]), its maximum number of char it can store is 9 characters? or does things work differently in a struct? It would be nice if someone can help me the concept because it seems like i may have been working with undefined behaviour this whole time.

2
What i was taught was that this array can store up to 10 characters (index 0-9) and that at index 10 there is an automatically placed null terminating character Either you were taught wrong or misheard. There are 10 characters total, including the null terminating character.Barmar

2 Answers

4
votes

char aString[10] = "";

What i was taught was that this array can store up to 10 characters (index 0-9)

Yes.

and that at index 10 there is an automatically placed null terminating character

That is wrong. For one thing, index 10 would be out of bounds of the array. The compiler will certainly not initialize data outside of the memory it has reserved for the array.

What actually happens is that the compiler will copy the entire string literal including the null-terminator into the array, and if there are any remaining elements then they will be set to zeros. If the string literal is longer than the array can hold, the compile will simply fail.

In your example, the string literal has a length of 1 char (the null terminator), so the entire array ends up initialized with zeros.

i know that accessing it would not be right

There is no problem with accessing the null terminator, as long as it is inside the bounds of the array.

such that if we use string handling functions (printf, scanf, strcmp, etc.) they would know when the string stops.

Yes, they expect C-style strings and so will look for a null terminator - unless they are explicitly told the actual string length, ie by using a precision modifier for %s, or using strncmp(), etc.

However when I tried making a struct data type like below,

<snip>

inserted 10 characters into accountNum (any method, e.g. scanf), and printf it, what is printed out will be accountNum and values in the first word of name

That means you either forgot to null-terminate accountNum, or you likely overflowed it by writing too many characters into it. For instance, that is very easy to do when misusing scanf(), strcpy(), etc.

i know that printf will stop at a space or a '\0'

printf() does not stop on a space, only on a null terminator. Unless you tell it the max length explicitly, eg:

CUSTOMER c;
strncpy(c.accountNum, "1234567890", 10); // <-- will not be null terminated!
printf("%.10s", c.accountNum); // <-- stops after printing 10 chars!

If it has not encountered a null terminator by the time it reaches the 10th character, it will stop itself.

This indicates that a char array does not have a terminating null at the end of the array.

An array is just an array, there is no terminator, only a size. If you want to treat a character array as a C-style string, then you are responsible for making sure the array contains a nul character in it. But that is just semantics of the character data, the compiler will not do anything to ensure that behavior for you (except for in the one case of initializing a character array with a string literal).

Does this mean that if we have a char array of size 10 (char aString[10]), its maximum number of char it can store is 9 characters?

Its maximum storage will always be 10 chars, period. But if you want to treat the array as a C-style string, then one of those chars must be a nul.

or does things work differently in a struct?

No. Where an array is used does not matter. The compiler treats all array the same, regardless of context (except for the one special case of initializing a character array with a string literal).

1
votes

What i was taught was that this array can store up to 10 characters (index 0-9) and that at index 10 there is an automatically placed null terminating character (i know that accessing it would not be right) such that if we use string handling functions (printf, scanf, strcmp, etc.) they would know when the string stops.

Yes, but accessing the null terminating character is absolutely safe.

inserted 10 characters into accountNum (any method, e.g. scanf), and printf it, what is printed out will be accountNum and values in the first word of name (i know that printf will stop at a space or a '\0'). This indicates that a char array does not have a terminating null at the end of the array.

printf does not stop for a space, only for a null terminating character. In this case, printf will print all characters until it sees '\0'.

Does this mean that if we have a char array of size 10 (char aString[10]), its maximum number of char it can store is 9 characters?

Yes.

or does things work differently in a struct?

There is no difference.