Question: The problem is below, but this is what I think is happening: We are taking a copy of the pointer and allocating it in memory
The problem is below, but this is what I think is happening:
We are taking a copy of the pointer and allocating it in memory so that we can use it. We are doing that in the allocate_node function when it is called in main with allocate_node(p);
The problem is that when it comes over to main the pointer is unitialized right?
How do I initialize it?
Main should be an int, and I think allocate_node should not be a void. But I don't know how to fix this issue.
Thanks for any help.
//*********************************************
Consider the following program, written in C:
typedef struct
{
int x;
int y;
} Foo;
void allocate_node (Foo * f)
{
f = (Foo *) malloc ( sizeof(Foo) );
}
void main ()
{
Foo * p;
allocate_node (p);
p->x = 2;
p->y = 3;
free(p);
}
Although the program compiles, it produces a run-time error. Why?
Rewrite the two functions allocate_node and main so that the program runs correctly.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
