Difference between “pointer to int” and “pointer to array of ints”

前端 未结 8 1494
甜味超标
甜味超标 2020-11-29 02:48
int main()
{
    int (*x)[5];                 //pointer to an array of integers
    int y[6] = {1,2,3,4,5,6};    //array of integers
    int *z;                              


        
8条回答
  •  無奈伤痛
    2020-11-29 03:41

    Hope this code helps.


    #include 
    #include 
    #define MAXCOL 4
    #define MAXROW 3
    
    int main()
    {      
          int i,j,k=1;
          int (*q)[MAXCOL];      //pointer to an array of integers
    
          /* As malloc is type casted to "int(*)[MAXCOL]" and every 
             element (as in *q) is 16 bytes long (I assume 4 bytes int), 
             in all 3*16=48 bytes will be allocated */
    
          q=(int(*)[MAXCOL])malloc(MAXROW*sizeof(*q)); 
    
          for(i=0; i

提交回复
热议问题