142 views

$1)$ How to access array element with array of pointers?

By pointer to an array we can access like this $(*a)$,$(*a)$,….. like this

right?

but how with array of pointer?

$2)$Check these three codes , one of them use array and one of them use array of pointer, and 3rd one using double pointer, but all giving same output. Why??

#include <stdio.h>
int ptr;
int main(void) {
if(*(ptr+5)==*(ptr+3)){
printf("Equal");
}
else{
printf("Not Equal");
}
return 0;
}

#include <stdio.h>
int *ptr;
int main(void) {
if(*(ptr+5)==*(ptr+3)){
printf("Equal");
}
else{
printf("Not Equal");
}
return 0;
}
#include <stdio.h>
int **ptr;
int main(void) {
if(*(ptr+5)==*(ptr+3)){
printf("Equal");
}
else{
printf("Not Equal");
}
return 0;
}
| 142 views
0
maa'm
i think here we defined array of pointer but not assign array value..........so in IF condition takes default value....

so Output: Equal

but i am not sure??
0

Program 1

int ptr;

ptr is the name of the array which acts as a constant pointer that points to the first element of the array. i.e. stores the address of $1^{st}$ element.

(ptr+i) means move i address locations forward. $(ptr+i)^{*}$ means access the value present at that location.i.e. ptr[i] or i[ptr].

For eg if array's $1^{st}$ element is stored at 100 and int takes 2 bytes then (ptr+5) = 100 + 2*5 = 110 i.e. the address of the  $6^{th}$ element of the array and $(ptr+5)^{*}$ means access the value present at location 110. i.e ptr.

*(ptr+5)==*(ptr+3)

Since we have declared ptr as global. So by default each element of the array will store 0 so this returns true.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program 2

int *ptr;

Here ptr is an array of size 12 where each element of the array i.e. ptr[i] is a pointer that can point to an integer value.

So (ptr+i) means we move to the address location which is i steps ahead in the array.

For eg if array's $1^{st}$ element(i.e. a pointer) is stored at 1000 and int pointer takes 2 bytes then (ptr+5) = 1000 + 2*5 = 1010 i.e. the address of the  $6^{th}$ element(which is also a pointer i.e. stores an address) and $(ptr+5)^{*}$ means access the value present at location 1010. i.e ptr which is also a location.

*(ptr+5)==*(ptr+3)

Since we have declared ptr as global. so by default each pointer i.e. each element of the pointer array will point to NULL so this returns true.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program 3

int **ptr;

Here ptr is an array of size 12 where each element of the array i.e. ptr[i] is a pointer that can point to a 1D array.

So (ptr+i) means we move to the address location which is i steps ahead in the array.

For eg if array's $1^{st}$ element(i.e. a pointer to 1D array) is stored at 1000 and int pointer takes 2 bytes then (ptr+5) = 1000 + 2*5 = 1010 i.e. the address of the  $6^{th}$ element(which is also a pointer to 1D array i.e. stores the address of starting location of an array) and $(ptr+5)^{*}$ means access the value present at location 1010. i.e ptr which is also a address of 1D array.

*(ptr+5)==*(ptr+3)

Since we have declared ptr as global. so by default each pointer i.e. each element of the pointer array will point to NULL so this returns true.

+1 vote

Program 1

int ptr; $\rightarrow$ ptr is the name of the array which acts as a constant pointer that points to the first element of the array. i.e. stores the address of 1st element.

$\rightarrow$   (ptr+i) means move i address locations forward.

$\rightarrow$  (ptr+i)∗ means access the value present at that location.i.e. ptr[i] or i[ptr].

$\rightarrow$ For eg if array's 1st element is stored at 100 and int takes 2 bytes then (ptr+5) = 100 + 2*5 = 110 i.e. the address of the  6th element of the array.

$\rightarrow$ (ptr+5)∗ means access the value present at location 110. i.e ptr.

*(ptr+5)==*(ptr+3)

$\rightarrow$ Since we have declared ptr as global. So by default each element of the array will store 0 so this returns true.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program 2

int *ptr; $\rightarrow$  Here ptr is an array of size 12 where each element of the array i.e. ptr[i] is a pointer that can point to an integer value.

$\rightarrow$  So (ptr+i) means we move to the address location which is i steps ahead in the array.

$\rightarrow$ For eg if array's 1st element(i.e. a pointer) is stored at 1000 and int pointer takes 2 bytes then (ptr+5) = 1000 + 2*5 = 1010 i.e. the address of the  6th element(which is also a pointer i.e. stores an address)

$\rightarrow$ (ptr+5)∗ means access the value present at location 1010. i.e ptr which also contains a location(address).

*(ptr+5)==*(ptr+3)

$\rightarrow$ Since we have declared ptr as global. so by default each pointer i.e. each element of the pointer array will point to NULL so this returns true.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

Program 3

int **ptr; $\rightarrow$ Here ptr is an array of size 12 where each element of the array i.e. ptr[i] is a pointer that can point to a 1D array.

$\rightarrow$ So (ptr+i) means we move to the address location which is i steps ahead in the array.

$\rightarrow$ For eg if array's 1st element(i.e. a pointer to 1D array) is stored at 1000 and int pointer takes 2 bytes then (ptr+5) = 1000 + 2*5 = 1010 i.e. the address of the  6th element(which is also a pointer to 1D array i.e. stores the address of starting location of a 1D array)

$\rightarrow$ (ptr+5)∗ means access the value present at location 1010. i.e ptr which is also a address of 1D array.

*(ptr+5)==*(ptr+3)

$\rightarrow$ Since we have declared ptr as global. so by default each pointer i.e. each element of the pointer array will point to NULL so this returns true.

$\rightarrow$  Suppose if the ptr stores the address of a 1D that contains 5 elements and we want to obtain the value of 2nd element in that 1D array i.e a then we would write like

*(*(ptr+3) + 1)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

NOTE:

In program 2 each element of ptr array contains the address of an integer value whereas in program 3 each element contains the address of a 1D array of integer type.

by Boss (25.1k points)
selected by
0

@Satbir

Can u draw a diagram, and show how all pointer returning $0.$

Pointer returning NULL, doesnot always mean it will return value $0$, right??

0

Pointer returning NULL means they are pointing to NULL and not to an address location so it will always return 0 since ASCII value of NULL is 0. Once you initialize the pointers with address then they will return address to which they are pointing.

0

@Satbir

One more question, pointer is NULL, that means it is not pointing any location

Does it not mean *(ptr+5),*(ptr+3) place of array not addressed by any pointer. So, *(ptr+5),*(ptr+3) can contain garbage value??

0

In program1 if ptr is not global then  *(ptr+5) will return garbage value. Since an array is created but not initialized. And we know that when array is created  then it contains garbage value.

In program 2 and program 3 if ptr is not global then they will not contain any value. So when we do

if(*(ptr+5)==*(ptr+3))

This will give an error and program will stop. Since there is nothing inside the pointer to compare...not even NULL.Here the pointers are only created and not initialized i.e they are pointing to nothing not even pointing to a garbage value's location  or any garbage address's location.