Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Understanding Groups

Answered: 1 week ago