Array of pointers to arrays

前端 未结 4 1903
温柔的废话
温柔的废话 2021-01-05 03:47

I am new to C programming and this is my problem:

I want to store the first value of each array in a new array, then the second value of each array in a new array an

4条回答
  •  天涯浪人
    2021-01-05 03:59

    int* (*a[5])[5][5][5] declares an array of 5 pointers to a 3d array of pointers to ints

    int* (*(*a[5])[5])[5][5][5] declares an array of 5 pointers to an array of 5 pointers to a 3d array of pointers to ints.

    #include 
    
    int main()
    {
        int t1[4]={0,1,2,3};
        int t2[4]={4,5,6,7};
        int t3[4]={8,9,10,11};
        int t4[4]={12,13,14,15};
        int (*tab[4])[4]={&t1,&t2,&t3,&t4};
        int i,j,k,l;
        for (i=0; i<4;i++)
        {
            printf("%d\t", (*tab[i])[1]);
        }
    
        return 0;
    }
    

    There's a difference between t2 and &t2. Though they have the same value their types are different. int [4] vs int (*)[4]. The compiler will throw a warning (clang) or error (gcc).

    int a[4] is conceptually at compiler level a pointer to an array of 4 as well as being the array itself (&a == a).

    int (*a)[4] is conceptually at compiler level a pointer to a pointer to an array of 4 as well as being a pointer to the array itself (a == *a) because it's pointing to an array type where the above is true.

提交回复
热议问题