C Programming - Pointers - Discussion
Discussion Forum : Pointers - Point Out Correct Statements (Q.No. 4)
4.
In the following program add a statement in the function fun() such that address of a gets stored in j?
#include<stdio.h>
int main()
{
int *j;
void fun(int**);
fun(&j);
return 0;
}
void fun(int **k)
{
int a=10;
/* Add a statement here */
}
Discussion:
19 comments Page 1 of 2.
Yash borse said:
12 months ago
This C code snippet demonstrates the use of pointers and functions. Here's a clear and concise explanation:
Header Inclusion: The code begins with #include<stdio.h>, which includes the standard input-output library necessary for using functions like printf.
Main Function: The main() function is the entry point of the program. Inside it:
An integer pointer j is declared (int *j;).
A function prototype for fun is declared, which takes a pointer to a pointer to an integer (void fun(int**);).
The fun function is called with the address of j (fun(&j);), passing a pointer to the pointer j.
Function Definition: The fun function is defined to accept a double pointer (int **k). Inside this function:
An integer variable a is initialized with the value 10.
The comment /* Add a statement here */ indicates where additional code can be added to manipulate or use the variable a or the pointer k.
Key Concepts:
Pointers: j is a pointer to an integer, and k is a pointer to a pointer to an integer. This allows for dynamic memory management and manipulation of variables at different levels of indirection.
Function Parameters: The function fun can modify the pointer j indirectly through the pointer k.
Example of Adding a Statement:
If you wanted to assign the value of a to the location pointed to by k, you could add the following line in the fun function.
Header Inclusion: The code begins with #include<stdio.h>, which includes the standard input-output library necessary for using functions like printf.
Main Function: The main() function is the entry point of the program. Inside it:
An integer pointer j is declared (int *j;).
A function prototype for fun is declared, which takes a pointer to a pointer to an integer (void fun(int**);).
The fun function is called with the address of j (fun(&j);), passing a pointer to the pointer j.
Function Definition: The fun function is defined to accept a double pointer (int **k). Inside this function:
An integer variable a is initialized with the value 10.
The comment /* Add a statement here */ indicates where additional code can be added to manipulate or use the variable a or the pointer k.
Key Concepts:
Pointers: j is a pointer to an integer, and k is a pointer to a pointer to an integer. This allows for dynamic memory management and manipulation of variables at different levels of indirection.
Function Parameters: The function fun can modify the pointer j indirectly through the pointer k.
Example of Adding a Statement:
If you wanted to assign the value of a to the location pointed to by k, you could add the following line in the fun function.
Ajay said:
9 years ago
**k will store value, whereas *k will store address.
Because int **k is pointer to pointer to the integer. When we apply once * on k we will get an address and on applying * second time on k we get value at (*k) address.
Because int **k is pointer to pointer to the integer. When we apply once * on k we will get an address and on applying * second time on k we get value at (*k) address.
(1)
Priya said:
8 years ago
Because a is only the name of the variable at which actual value is stored i.e. 10.
And to access 10 from a location we use its address in this fun i.e &a.
And to access 10 from a location we use its address in this fun i.e &a.
Prabhu said:
1 decade ago
By passing &j as argument,k has the address of j. *k is the value stored at j.so assignment statement *k=&a, stores address of a as value of j.
Deepti said:
1 decade ago
Its normal pointer assignment, whn fun is called its received as k, so whatever operation performed on k will be returned at j in main.
Mohan said:
6 years ago
Simple, k contains address of j that is &j
*k = &a means *(&j) = &a
So finally j = &a.
*k = &a means *(&j) = &a
So finally j = &a.
(2)
PAVAN said:
1 decade ago
AFTER DUNC CALL,
&J=**K;
J=*K;
IN FUNC FUN,
*K=&A;
SO,
J=&A;
SO ADDRESS OF A IS IN J
&J=**K;
J=*K;
IN FUNC FUN,
*K=&A;
SO,
J=&A;
SO ADDRESS OF A IS IN J
Meghali said:
1 decade ago
&J=**K
So, j=*K
We are assigning address of a to j.
So , J=&a
i.e *k=&a
So, j=*K
We are assigning address of a to j.
So , J=&a
i.e *k=&a
ARTASH said:
4 years ago
Please anyone explain about /* add a statement here */.
I want to see the output.
I want to see the output.
Ted said:
1 decade ago
But there is no semicolon after the statement in the answer?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers