3

Here is a C program in textbook, it asks a 3*5 2D array from users and prints the third line.

I am confused with int* p[5]. Why here needs to have [5], I think just int* p is OK. It can repeatedly add and point to the next memory space in the int array. And can anyone explain how pointer works in this program?

#include <stdio.h>

int main(void){
        int a[3][5];
        int i,j;
        int *p[5];

        p = &a[0];
        printf("Please input:\n");
        for(i = 0; i < 3; i++){
                for(j = 0; j<5;j++){
                        scanf("%d\n",(*(p+i))+j);
                }
        }
        p = &a[2];
        printf("the third line is:\n");
        for(j = 0; j<5; j++){
                printf("%5d", *((*p)+j));
        }
        printf("\n");
}
alk
  • 69,737
  • 10
  • 105
  • 255
Jennifer Q
  • 257
  • 3
  • 12
  • In C a 2D array is just an array of a arrays. – alk Mar 20 '16 at 08:12
  • You are correct. No need for `p[5]` here. Clearly, there is no assignment to any `p[i]`. – barak manos Mar 20 '16 at 08:12
  • 1
    `p = &a[0];` should not compile. – user253751 Mar 20 '16 at 08:12
  • While it might work, it is still wrong and useless. Why not directly index into the 2D array? Anyway, `int *p[5]` is the wrong type. Just another author having trouble with arrays and pointers and the syntax in C. A pointer to the inner array would be `int (*p)[5]` - notice the parenthesis! – too honest for this site Mar 20 '16 at 08:14
  • But when I change it to int*p, it reports (*(p+i))+j) becomes to type int rather than int *. I just want to know what happens here? – Jennifer Q Mar 20 '16 at 08:20
  • @Olaf: Sry, but this comment "*Just another author having trouble ...*" is just useless, because that's why we are here. – alk Mar 20 '16 at 08:23
  • @alk: There is a difference if a beginner writes such code or a book author. – too honest for this site Mar 20 '16 at 08:26
  • @Olaf: Ahok, *if* this *is verbatim* from a "book" I understand and follow your comment ... - I thought you were referring to the author of this question, so please excuse. – alk Mar 20 '16 at 08:32
  • @immibis: Correct. Cleaning up. – alk Mar 20 '16 at 09:09

1 Answers1

3
int *p[5];

is an array of five pointers to int.

What you want is a pointer to an array of five ints

int (*p)[5];

because &a[0] is the address of the 1st element of a which is an int[5].

The compiler should have clearly issued at least a warning on this, if not an error, which would be expected.

More on this here: C pointer to array/array of pointers disambiguation

Community
  • 1
  • 1
alk
  • 69,737
  • 10
  • 105
  • 255
  • yep, it reports a warning. There should be parenthesis. But most confused me is when I change it to int*p, (*(p+i))+j) becomes to type int rather than int *. Why that happens? – Jennifer Q Mar 20 '16 at 08:26
  • 1
    @JenniferQ appying `*` to `int *` gives `int` – M.M Mar 20 '16 at 08:31
  • @JenniferQ: `int * p` defines `p` to point to an `int`. If you apply the de-referencing operator `*` to `p` you get what is points to, that is an `int` here. – alk Mar 20 '16 at 08:34