简体   繁体   中英

Array pointers assignmnet to 2-D array

void  main()
  {
    int a[][4]={ 5,7,5,9,
                 4,6,3,1,
                 2,9,0,6};
    int *p;
    int *q [4];
    p=(int *)a;
    q=a;
    printf("\n %u %u",p,q);
    p++;
    q++;
    printf("\n %u %u",p,q);
}

My query is can we assign 2-D array to the array of pointers. The above code shows error as 1.c: In function 'main': 1.c:14:6: error: assignment to expression with array type

In C language, arrays are not first class citizens:

  • an array decays to a pointer when it is used in an expression
  • you can never assign to an array

If q is an array of pointer, the answer is no, you cannot assign to an array.

If you want q to be a pointer to arrays , you must declare it as int (*q)[4]; :

int(*q)[4];
q = a;

You can then use it to access elements of a : q[i][j] is the same as a[i][j] (and q[i] the same as q[j] )

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