C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 19)
19.
What will be the output of the program ?
#include<stdio.h>
power(int**);
int main()
{
    int a=5, *aa; /* Address of 'a' is 1000 */
    aa = &a;
    a = power(&aa);
    printf("%d\n", a);
    return 0;
}
power(int **ptr)
{
    int b;
    b = **ptr***ptr;
    return (b);
}
5
25
125
Garbage value
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
25 comments Page 1 of 3.

Bhavya b.c said:   1 decade ago
The variable a contains the value 5 and address of a is 1000;
a=5;
*aa is pointer variable
aa=&a;

So aa contains address of a therefore *aa=5.
when the call statement a=power(&aa) invoked the control transfer to

The formal parameter that is power(int **ptr)
so the value of *aa is assign to the **ptr means **ptr=5.

b=**ptr***ptr;

There are no three astricks the middle one * means multiplication.
b=5*5
b=25 .
(1)

Prakash g said:   1 decade ago
#include<stdio.h>
power(int**);
int main()
{
int a=5, *aa; /* Address of 'a' is 1000 */
aa = &a;
a = power(&aa);
printf("%d\n", a);
return 0;
}
power(int **ptr)
{
int b;
b = **ptr***ptr;
return (b);
}

In that **p means value and afre that * is for multification. Again **ptr means values.

So it return directly value ...simple.

Deepak said:   7 years ago
@All.

Let us consider that a=5.aa=&a i.e, if a is storing at 500th location, means aa=500 and as we can see in the above prog that we are sending the address of pointer in function call i.e suppose to consider the address of pointer is 600 i.e &aa=600 so in order to access to value i=5 we dereference it by two times hence they have used **ptr.

Jogamohan Medak said:   1 decade ago
Given a=5;
So, aa=&a;//*aa=*&a=a=5;
//aa contains address of a=1000;
//&aa contains address of 1000 which is

b=**ptr***ptr;
=(**ptr)*(**ptr); //ptr = &aa;*ptr=*&aa=aa;
=(5)*(5); //**ptr=**aa=5 ;
=25;

Ans: [B] 25
(5)

Kavyashree said:   1 decade ago
aa = &a; // *aa = 5;

**ptr = &aa; ptr is a pointer to a pointer aa.

Hence value stored in a can be accessed using **ptr(which is **ptr = 5).

So the b = **ptr ***ptr means b = 5*5 which is 25.

Akshay said:   1 decade ago
int(**ptr)
aa=&a,so *aa=5
where ptr=&aa, hence we can say,**&aa.
since, * and & operator cancels each other we are left with
*aa which is 5
hence, 5*5=25

Rupinderjti said:   1 decade ago
The thing here need to take under consideration is that function is being called by reference. So change of value in function definition will appear in main. That's all.

Manjunath said:   1 decade ago
You may wonder how is the function power returning an integer even though its prototype is different. In c by default the return type of any function is int.

Dinesh said:   1 decade ago
aa=&a//aa contains address of a=1000;
&aa contains address of 1000 which is
any value
so**aa containns 5;
hence **ptr contains 5
and **ptr***ptr=25

Lalit said:   1 decade ago
This answer is wrong because scope of variable b is local in function so this variable automatically remove from memory when function close.


Post your comments here:

Your comments will be displayed after verification.