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 2 of 2.
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.
Ketaki said:
7 years ago
I cannot understand. Please explain in detail.
Anoop Mehra said:
7 years ago
C option is correct but it should be *k=&a;.
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)
Sakshi said:
6 years ago
Didn't get, can anyone explain, please.
Omar said:
5 years ago
This is dangling pointer.
Priya said:
5 years ago
Can anyone explain it, please?
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers