2 2 votes int b[10][10]; int *a[10]; //How b[2][3] and a[2][3] will work?? Programming in C + – jatin khachane 1 3.0k views answer comment Share Follow Print 0 reply Please log in or register to add a comment.
Best answer 4 4 votes I am assuming pointer size and integer size = $4$ Bytes. /* We know any ptr + k = value_of(ptr) + k * sizeof(*ptr); Now, b[2][3] = *(*(b+2)+3) Type of b is int (*)[5] The initial value of b is 1000 with type int (*)[5] b + 2 = 1040 with Type int (*)[5] *(b + 2) = 1040 with Type int * *(b + 2) + 3 = 1052 with type int * *(*(b + 2) + 3) = [1052] = scan 4 bytes from 1052 and get the value which is an int int *a[5]; a[2][3] = *(*(a+2)+3) Type of a is int ** The initial value of a is 2000 with Type int ** a + 2 = 2008 with Type int ** *(a + 2) = [2008] scan 4 bytes from 2008 and get the value which is an int * = 1040 of Type int * *(a + 2) + 3 = 1052 with type int * *(*(a + 2) + 3) = [1052] = scan 4 bytes from 1052 and get the value which is an int */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> int main() { srand(time(NULL)); int b[5][5],i,j; int *a[5]; for(i = 0;i<5;i++) { for(j = 0;j<5;j++) { b[i][j] = rand()%100; } } printf("Array b : \n \n"); // printing the original b[][] array for(i = 0;i<5;i++) { printf("\t"); for(j = 0;j<5;j++) { printf("%3d ",b[i][j]); } printf("\n\n"); } // int *a[5] : a is an array of 5 integer pointers // assign these pointers with starting addresses of rows of b // Type of b[i] = int * for(i = 0;i<5;i++) { a[i] = b[i]; } printf("a[2][3] = b[2][3] = %d\n",a[2][3]); } /* //output Array b : 43 98 68 87 90 67 34 49 51 35 41 33 25 91 96 86 53 8 16 93 68 21 43 20 24 a[2][3] = b[2][3] = 91 */ a can be used for other purposes as well other than accessing a $2D$ array elements dd answered Jul 2, 2017 • selected Jul 3, 2017 by Arjun dd comment Share Follow See all 18 Comments 18 18 Comments reply jatin khachane 1 commented Jul 2, 2017 reply Follow flag How can (b+2) and *(b+2) will be same that is 1040 0 0 replyShare Hemant Parihar commented Jul 3, 2017 reply Follow flag printf("%x, b); printf("%x, *b); printf("%x, &b); All three will print the same address. 1 1 replyShare srestha commented Jul 3, 2017 reply Follow flag @Debashish @Hemant if we only declare int *a[10]; and want to print a[2][3] , it will give error. rt? Though we can say *a[10] is a pointer to an array, and can work like 2D array , but it cannot itself contain a value like a 2D array. So, without this conversion in the above code it will give error. right? for(i = 0;i<5;i++) { a[i] = b[i]; } //printf("a[2][3] = b[2][3] = %d\n",a[2][3]); 0 0 replyShare jatin khachane 1 commented Jul 3, 2017 reply Follow flag Is this holds for row measure order also...as in it elements are stored in one 1D array only 0 0 replyShare Hemant Parihar commented Jul 3, 2017 reply Follow flag @srestha int *a[10] is an array of pointer. Here we are storing a pointer to int at a[0], a[1], a[2], ...... a[9]. The pointer at a[0] points to an int. It can also point to an element of an array. And by incrementing this, we can access next element of an array. Because of this, we can have variable length two-dimensional arrays. If you don't store the pointer at the location a[0]. Then most likely there will be a null value or garbage value. So you must need to initialize them before using them. 1 1 replyShare srestha commented Jul 3, 2017 reply Follow flag I am not telling , a[0] not storing any address. I told if we initialize int *a[10]; and then want to access a[2][3] (when no 2 D array initialized before it), then it will give error. because a[2][3] is a constant value. And *a[10] is just a pointer. So, cannot store any value. Just it can access a value. right? *a[10] can access 2 D array element , but cannot store 2D array element, that I want to tell. 0 0 replyShare Hemant Parihar commented Jul 3, 2017 i edited by Hemant Parihar Jul 3, 2017 reply Follow flag @debashish can you verify, i'm correct or wrong. /* Pointer to an array */ #include<stdio.h> #include<stdlib.h> int main() { int a[5] = {1, 2, 3, 4, 5};/* Here a is not pointing to an array of size 5, but it is pointing at the first location of the array. That's why its type is int *.*/ int (*b)[5] = &a;/* Here b is a pointer to an array[5]. Type of b is int (*)[5] */ /* Here if you trying to do int (*b)[4] = &a. Then it will give error: cannot convert int (*)[5] to int (*)[4].*/ int c[3][3]= {1, 2, 3, 4, 5, 6, 7, 8, 9}; /* Here c is pointing to the 1st location, which is an array of 3 interger element. So its type is int (*)[3] */ /* (c + 1) will point to first element of the second row*/ int (*d)[3] = c; // d is a pointer to an array consisting 3 interger element. // All these should print the starting address of array a. printf("%x\n", a); printf("%x\n", &a); printf("%x\n", b); // Accessing the 2 element of array a. printf("%d\n", *(a + 1)); printf("%d\n", *(*(b) + 1)); /*Here we need to dereferencing b two times to access an element. Because b is pointing to an array[5] and b's type is int (*)[5].*/ // Accessing the c[1][2]. printf("%d\n", *(*(c + 1) + 2) ); printf("%d\n", *(*(d + 1) + 2)); } 1 1 replyShare srestha commented Jul 4, 2017 reply Follow flag Can u tell me the logic? Code I shall see later. 2D pointer can point an element of 2D array. 2D pointer can point an element of 1D array. 1D pointer cannot point to an element of 2D array. Is these statements are all true? 0 0 replyShare Prashant. commented Jul 4, 2017 i edited by Prashant. Jul 4, 2017 reply Follow flag 2D pointer can point an element of 2D array. True (Trivial) 2D pointer can point an element of 1D array. True b[][] = &p; *p = C[]; 1D pointer can point to an element of 2D array. True Need TypeCasting. 0 0 replyShare Hemant Parihar commented Jul 4, 2017 reply Follow flag Using type casting. A pointer can point to an element of a 1D array, 2D array, 3D array so on. #include<stdio.h> int main() { int a[2][3] = {1, 2, 3, 4, 5, 6}; int *b; b = (int *)a; // b is pointing to first row 1 column. printf("%d\n", *b); } 0 0 replyShare Prashant. commented Jul 4, 2017 reply Follow flag yes typecasting makes it :) 1 1 replyShare srestha commented Jul 4, 2017 reply Follow flag int *b; b = (int *)a; // b is pointing to first row 1 column. So, here u r taking declaring b as pointer and again typecasting it with integer pointer. So, it is again a double pointer. Isnot it? So, 1D pointer cannot point to an element of 2D array. isnot it true? 0 0 replyShare Hemant Parihar commented Jul 4, 2017 reply Follow flag @srestha b is not type cast it is actually a. Once you define the type it can't be changed. Typecasting doesn't change the type of the variable. 0 0 replyShare srestha commented Jul 4, 2017 reply Follow flag yes, that is correct. But u r casting a with integer pointer and storing it inside b. So, does it not mean, Now value of b (Not *b) is similar as single pointer value of a ? 0 0 replyShare srestha commented Jul 4, 2017 reply Follow flag @Hemant See this question https://gateoverflow.in/893/gate2003-2 here for B[2][3]=5 // So, LHS assignment for B[2][3] is 5 Similarly what can be LHS assignment for A[2] , A[2][3] , B[1] ? 0 0 replyShare Hemant Parihar commented Jul 8, 2017 reply Follow flag @srestha https://gateoverflow.in/136596/pointers you can look at this for more. 0 0 replyShare srestha commented Jul 24, 2017 reply Follow flag @Hemant plz specifically answer my last query. I understand that pointer question u told 0 0 replyShare Hemant Parihar commented Jul 26, 2017 reply Follow flag @srestha, I show the LHS assignment for A[2][3], A[2] in this code. Please run the code. For B[1] you can't have LHS assignment. B is the base address of the array. You can't make B to point any elsewhere. #include<stdio.h> int main() { int *A[10]; // A is an array of pointer. Type of A is int *(*). int a[5] = {1, 2, 3, 4, 5}; // 'a' decays to the type int *. A[2] = a; // A[0] will have type int *. 'a' also have same type so this is valid. int **p2 = (A + 2); // p2 and A both have same type int **. printf("%d\n", **p2); printf("%d\n", A[2][3]); A[2][3] = 10; printf("%d\n", A[2][3]); } 0 0 replyShare Please log in or register to add a comment.
0 0 votes Same as Question GATE CS 2013 Q No.-02. https://gateoverflow.in/893/gate2003-2 Deepak Kumar 12 answered Jul 2, 2017 Deepak Kumar 12 comment Share Follow 0 reply Please log in or register to add a comment.