简体   繁体   中英

char array in structs - why does strlen() return the correct value here?

I have a simple program like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct 
{
    int numberOfDays;
    char name[10];
} Month;


int main(void) 
{
    const Month months[12] = { 
        { 31, {'J', 'a', 'n'} },
        { 28, {'F', 'e', 'b'} }
    };

    printf("%zu\n", strlen(months[0].name));
    printf("%zu\n", sizeof(months[0].name));

    printf("%zu\n", strlen(months[1].name));
    printf("%zu\n", sizeof(months[1].name));

    return 0;
}

The output is like this:

3
10
3
10

I understand why sizeof(months[i].name) prints 10, but why does strlen return the correct value in this case?

My thought was, that strlen counts until the first '\\0' , but the char name[3] array is not null terminated. From my understanding this should be undefined behaviour? Does it only work by accident?

I'm wondering what the memory layout is in the above months[12] array.

TL;DR Answer: No, this is well-defined behaviour.

Explanation: As per the C11 standard document, chapter 6.7.9, initalization,

If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

In your case, you have a char array of 10 elements

 char name[10];

and you've supplied initializer for only 3 elements, like

{ 31, {'J', 'a', 'n'} },

So, the rest of the elements in name is initialized to 0 or '\\0' . So, in this case, strlen() returns the correct result.

Note: Please do not rely on this method for null-termination of strings . In case, you're supplying the exact number of elements as initalizer, there will be no null-termination.


EDIT:

In case the name definition is changed to char name[3] and initialized with three char s, then , as per the note above, usage of strlen() (and family) will be undefined behaviour as it will overrun the allocated memory area in search of terminating null.

The reason is that your months are indeed nul-terminated. If you have an array with 10 elements, and have an initialiser for 3 elements, then the rest is filled with 0's. If you had a month with 11 characters, the compiler would tell you. If you had a month with 10 characters, you would be in trouble because there would be no nul-termination, and the compiler wouldn't tell you.

When you partially initialise the struct , those parts not specifically initialised are set to 0.

So the strings do have a terminating 0 and so strlen() returns the correct value.

#include <stdio.h>
#include <string.h>

int main(){
int i;
    char s[10] = {'a', 'b', 'c'};
    for (i=0; i<10; i++)
        printf("%d ", s[i]);
    printf("\n%d\n", strlen(s));
    return 0;
}

Program output:

97 98 99 0 0 0 0 0 0 0
3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM