Difference between *ptr[10] and (*ptr)[10]

前端 未结 6 1416
自闭症患者
自闭症患者 2020-12-04 17:29

For the following code:

    int (*ptr)[10];
    int a[10]={99,1,2,3,4,5,6,7,8,9};
    ptr=&a;
    printf(\"%d\",(*ptr)[1]);

What should

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 17:34

    int (*p)[10] is a pointer to an array of 10 integers in each row i.e there can be any number of rows. basically it can be used to point to a 2D array and the dimensions can be accessed by incrementing i for *(p+i)[10] which is same as a[i][10], here 'i=1,2,3...' to access 1,2,3.. rows.

    where as, int *p[10] is an array of 10 integer pointers.

    If array is two dimesional i.e, for example

    b[2][10]={{0,1,2,3,4,5,6,7,8,9},{10,11,12,13,14,15,16,17,18,19}}; basically, (*ptr)[10] // where the higher dimension index is omitted i.e, 2 can be used as two dimensional pointer to an array(containing 10 elements in each row i.e, the 1st dimension) like (*ptr)[10] = &b. In the printf("%d",(*ptr)[1]); as provided (*ptr) is same as *(ptr+0)evaluates to the first dimension i.e, b[0][0] which value is 0. like wise, to access the 2nd dimension of the array b[1][0] the expression will be **(ptr+1) or *(ptr+1)[0] or *(*(ptr+i)+j); // where i=1 and j=0 gives the first element of the 2nd dimension i.e, 10.

    I've answered it long so to understand it easy.

提交回复
热议问题