817 views
0 votes
0 votes

int B[2][3];
int *p = B; // why this is wrong?
int (*p)[3] = B; // why this is correct?

2 Answers

3 votes
3 votes
When you write B, then it is address of first element of B, Now first element of B is whole 1D array of 3 int, so address of first element can be stored in pointer which points to whole 1D array of 3 int, which is int (*p)[3], not int *p.
0 votes
0 votes

Here Both are correct, depends on what you really need.

#include<stdio.h>
int main(){
    int B[2][3] = {{14,16,18},{19,29,39}};
    int *p = B; // why this is wrong?
    int (*q)[3] = B; // why this is correct?
    printf("%d %d\n",p[0],p[5]);// here p is pointing to single element
    printf("%d \n",q[1][0]);// here q is pointing to whole 1 D array
}

Related questions

0 votes
0 votes
2 answers
2
radha gogia asked Aug 20, 2015
364 views
int *ptr;ptr=9 ; // this is an error so to rectify it we write :int *ptr=(void*)9; why is this stmt true , what happens on type casting this 9 to a void pointer type sinc...
0 votes
0 votes
1 answer
3
radha gogia asked Jul 21, 2015
305 views
char ch;scanf("%c",&ch) // after I do scanning of this variable then after I press enter key then it gets stored inside the buffer so can we print this enter key .Since d...