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

后端 未结 4 1781
耶瑟儿~
耶瑟儿~ 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

    Are all strings allocated the same amount of elements as the largest string in the definition?

    No, only 3 pointer are allocated and they point to 3 string literals.

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

    and

    char stuff[3][9];
    

    are not at all equivalent. First is an array of 3 pointers whereas the second is a 2D array.

    For the first only pointer are allocated and the string literals they point to may be stored in the read-only section. The second is allocated on automatic storage (usually stack).

提交回复
热议问题