Alex Rivera | Logout

Difference between "pointer to int" and "pointer to array of ints"

Asked 2010-03-07T20:18:04.790
32
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;                      //pointer to integer

    z = y;
    for(int i=0;i<6;i++)
        printf("%d ",z[i]);

    x = y;
    for(int i=0;i<6;i++)
        printf("%d ",(*x)[i]);

    return 0;
}

Both the above printfs print numbers 1 through 6.
If both "pointer to array of integers" and "pointer to integer" can do the same thing, do they have the same internal representation?
EDIT: This code does give warnings when compiled as pointed out by the answers below, however it does print the values correctly both the time on my x86_64 machine using gcc

c
Edit
Report

1 Answer

3

Who knows - this code exhibits undefined behavior:

printf("%d ",(*x)[i]);
answered 2010-03-07T20:24:34.123

Your Answer