398 views
int b;

int *a;

//How b and a will work??
| 398 views I am assuming pointer size and integer size =  $4$ Bytes.

/*
We know any ptr + k = value_of(ptr) + k * sizeof(*ptr);
Now,
b = *(*(b+2)+3)
Type of b is int (*)
The initial value of b is 1000 with type int (*)
b + 2 = 1040 with Type int (*)
*(b + 2) = 1040 with Type int *
*(b + 2) + 3 = 1052 with type int *
*(*(b + 2) + 3) =  = scan 4 bytes from 1052
and get the value which is an int

int *a;
a = *(*(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) =  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) =  = 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,i,j;
int *a;
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 : 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 = b = %d\n",a);
}
/*
//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 = b = 91

*/

a can be used for other purposes as well other than accessing a $2D$ array elements

by Veteran (57k points)
selected by
0
How can (b+2) and *(b+2) will be same that is 1040
+1
printf("%x, b);
printf("%x, *b);
printf("%x, &b);
All three will print the same address.
0

@Debashish @Hemant

if we only declare int *a;

and want to print a , it will give error. rt?

Though we can say *a 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 = b = %d\n",a);
0
Is this holds for row measure order also...as in it elements are stored in one 1D array only
+1
@srestha
int *a is an array of pointer.
Here we are storing a pointer to int at a, a, a, ...... a.
The pointer at a 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. Then most likely there will be a null value or garbage value. So you must need to initialize them before using them.
0
I am not telling , a not storing any address. I told if we initialize int *a; and then want to access a (when no 2 D array initialized before it), then it will give error. because a is a constant value. And *a is just a pointer. So, cannot store any value. Just it can access a value. right?

*a can access 2 D array element , but cannot store 2D array element, that I want to tell.
+1
@debashish can you verify, i'm correct or wrong.
/* Pointer to an array */
#include<stdio.h>
#include<stdlib.h>

int main() {
int a = {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) = &a;/* Here b is a pointer to an array. Type of b is int (*) */
/* Here if you trying to do int (*b) = &a. Then it will give
error: cannot convert int (*) to int (*).*/

int c= {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 (*) */
/* (c + 1) will point to first element of the second row*/

int (*d) = 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
and b's type is int (*).*/

// Accessing the c.
printf("%d\n", *(*(c + 1) + 2) );
printf("%d\n", *(*(d + 1) + 2));
}

0
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

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

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 = {1, 2, 3, 4, 5, 6};
int *b;
b = (int *)a; // b is pointing to first row 1 column.
printf("%d\n", *b);

}
+1
yes typecasting makes it :)
0
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
@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
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
@Hemant See this question
https://gateoverflow.in/893/gate2003-2
here for B=5 // So, LHS assignment for B is 5
Similarly what can be LHS assignment for A , A , B ?
0

@srestha
https://gateoverflow.in/136596/pointers
you can look at this for more.

0
@Hemant

plz specifically answer my last query.

I understand that pointer question u told
0

@srestha​​​​​​,
I show the LHS assignment for A, A in this code. Please run the code.

For B 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; // A is an array of pointer. Type of A is int *(*).
int a = {1, 2, 3, 4, 5}; // 'a' decays to the type int *.
A = a; // A 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);
A = 10;
printf("%d\n", A);
}


Same as Question GATE CS 2013 Q No.-02.

https://gateoverflow.in/893/gate2003-2

by Active (1.6k points)