How is memory allocated for an implicitly defined multidimensional array in C99?

后端 未结 4 1786
耶瑟儿~
耶瑟儿~ 2020-12-11 12:02

I\'m trying to write a C99 program and I have an array of strings implicitly defined as such:

char *stuff[] = {\"hello\",\"pie\",\"deadbeef\"};
4条回答
  •  醉话见心
    2020-12-11 12:25

    char *stuff[] = {"hello","pie","deadbeef"};
    

    Is not a multidimensional array! It is simply an array of pointers.

    how much memory is allocated for each string?

    The number of characters plus a null terminator. Same as any string literal.

    I think you want this:

    char foo[][10] = {"hello","pie","deadbeef"};
    

    Here, 10 is the amount of space per string and all the strings are in contiguous memory. Thus, there will be padding for strings less than size 10.

提交回复
热议问题