What is the difference between the following declarations in C?

后端 未结 4 1626
孤街浪徒
孤街浪徒 2021-01-15 06:44

what\'s the difference between these 2 declarations:

char (*ptr)[N];

vs.

char ptr[][N];

thanks.

4条回答
  •  难免孤独
    2021-01-15 07:15

    1.ptr is pointer to character array of size N 2. ptr looks like a double dimensional array but number of rows is not provided.In 2-D array both number of rows and column is compulsory as compiler will decide how many bytes should be assigned to array by (number of row*number of column*size of data type).This declaration may work fine as below:

    char[][2]={
               {'a','b'}
              };
    

    as here compiler will look and understand that there is one row.or when a two dimensional array is passed on as a function argument,the number of columns(second dimension) must be provided by compulsion,passing number of rows is not compulsory in function definition.

提交回复
热议问题