Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started