Question: Consider the following program, written in C: typedef struct { int x; int y; } Foo; void allocate_node (Foo * f) { f = (Foo
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
