0 votes
0 votes

 

Can you please explain the whole working of the code

in Programming in C
120 views

1 Answer

1 vote
1 vote

Memory Allocation Issue: In the fun function, you are attempting to allocate memory for an integer and assign it to the pointer a. However, the pointer a is passed by value, meaning that changes made to a inside the function do not affect the original pointer in main. Therefore, the memory allocation in fun does not have any impact on the pointer p in main. It does not allocate memory for p, and using *p = 6 leads to undefined behavior because p is uninitialized.

 

Now, fun takes a pointer to a pointer, and it correctly allocates memory for the integer. The address of p is passed to fun, so changes to *a inside fun affect the original pointer p in main. Also, it is important to free the allocated memory using free before the program exits to avoid memory leaks.

 

 

 

 

Related questions