C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 4)
4.
What will be the output of the program ?
#include<stdio.h>
void fun(int **p);
int main()
{
int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
int *ptr;
ptr = &a[0][0];
fun(&ptr);
return 0;
}
void fun(int **p)
{
printf("%d\n", **p);
}
Answer: Option
Explanation:
Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; The variable a is declared as an multidimensional integer array with size of 3 rows 4 columns.
Step 2: int *ptr; The *ptr is a integer pointer variable.
Step 3: ptr = &a[0][0]; Here we are assigning the base address of the array a to the pointer variable *ptr.
Step 4: fun(&ptr); Now, the &ptr contains the base address of array a.
Step 4: Inside the function fun(&ptr); The printf("%d\n", **p); prints the value '1'.
because the *p contains the base address or the first element memory address of the array a (ie. a[0])
**p contains the value of *p memory location (ie. a[0]=1).
Hence the output of the program is '1'
Discussion:
12 comments Page 1 of 2.
Prasanth said:
4 years ago
Please explain it clearly.
Sukeshni said:
5 years ago
Thanks @Rajat.
(1)
Rajat said:
6 years ago
Suppose that address of a[0][0] is 111 since a[0][0]=1.
Now, at ptr=&a[0][0];
Address 111 is stored in ptr
So, *ptr will be 1 and ptr will be 111.
At fun(&ptr), it will pass address of ptr to function, say 222
So in function implementation.
p will be 222
*p will be the value at address 222 which is 111.
**p will be value at address 111 which is 1.
So the answer is 1.
Now, at ptr=&a[0][0];
Address 111 is stored in ptr
So, *ptr will be 1 and ptr will be 111.
At fun(&ptr), it will pass address of ptr to function, say 222
So in function implementation.
p will be 222
*p will be the value at address 222 which is 111.
**p will be value at address 111 which is 1.
So the answer is 1.
(1)
Kaushal said:
1 decade ago
But there is not any return statement in fun (int**p). So how it can return to main() and exit?
Explain me.
Explain me.
Thanvi said:
1 decade ago
Why we use &a[0][0]?
Kushal Bajaj said:
1 decade ago
According to me 'ptr' contains base address of array 'a'.
Let us say for eg. 100. (i.e. ptr=100).
This means value '1'(i.e. a[0][0]) is stored at address location 100.
Now '&ptr' means address of 'ptr' for eg. lets say 200.
Now this address of ptr is passed to fun(&ptr);
Now *p means value at the address to which p points.
**p means value at which *p points.
As an eg.: if 200 address is passed to fun(); then,
*p would have pointed too 100 which is the address of ptr;
Now **p will point to the the value which is at location 100.
i.e. 1.
In other words, address location 200 contains the value 100.
this 100 is actually the location which contains the value '1'.
Hence, the statement,
printf("%d\n", **p);
Will print 1.
Let us say for eg. 100. (i.e. ptr=100).
This means value '1'(i.e. a[0][0]) is stored at address location 100.
Now '&ptr' means address of 'ptr' for eg. lets say 200.
Now this address of ptr is passed to fun(&ptr);
Now *p means value at the address to which p points.
**p means value at which *p points.
As an eg.: if 200 address is passed to fun(); then,
*p would have pointed too 100 which is the address of ptr;
Now **p will point to the the value which is at location 100.
i.e. 1.
In other words, address location 200 contains the value 100.
this 100 is actually the location which contains the value '1'.
Hence, the statement,
printf("%d\n", **p);
Will print 1.
Krishankant said:
1 decade ago
What is difference between *ptr and **ptr?
Rosy.IIIT said:
1 decade ago
Explain this clearly.
Vivek Agarwal said:
1 decade ago
In step 4,shouldn't &ptr hold the address of ptr itself,not the base address?? I think ptr holds base address of array 'a',not &ptr...Correct me if I'm wrong somewhere.
Devki said:
1 decade ago
Can you explain step 3 in detail. Why it is said that base address of a is assigned to *ptr when the statement is ptr=&a[][]?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers