The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
0 votes
86 views

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

By pointer to an array we can access like this $(*a)[0]$,$(*a)[22]$,….. 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[12];
int main(void) {
if(*(ptr+5)==*(ptr+3)){
printf("Equal");
}
else{
printf("Not Equal");
}
return 0;
}
#include <stdio.h>
int *ptr[12];
int main(void) {
if(*(ptr+5)==*(ptr+3)){
printf("Equal");
}
else{
printf("Not Equal");
}
return 0;
}
#include <stdio.h>
int **ptr[12];
int main(void) {
if(*(ptr+5)==*(ptr+3)){
printf("Equal");
}
else{
printf("Not Equal");
}
return 0;
}
asked in Programming by Veteran (111k points) | 86 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[12];

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[5].

*(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[12];

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[5] 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[12];

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[5] 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 Answer

+1 vote
Best answer

Program 1

int ptr[12];

$\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[5].

*(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[12];

$\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[5] 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[12];

$\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[5] 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[3] 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[1] 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.

answered by Boss (12.9k 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

@srestha please check my answer i have edited it.

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.

Related questions

0 votes
1 answer
4
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,540 questions
54,099 answers
187,269 comments
71,006 users