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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!