Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include 8 9 int * add (int a, int b) { 10 int result = a + b; 11 return & result ; 12 }

#include

8

9 int * add (int a, int b) {

10 int result = a + b;

11 return & result ;

12 }

13

14 int * multiply (int p, int q) {

15 int result = p * q;

16 return & result ;

17 }

18

19 int main () {

20 int * sum = add (4, 5);

21 printf ("sum = %dn", * sum );

22 int * product = multiply (2, 3);

23 printf (" product = %dn", * product );

24 printf ("sum - product = %d - %d = %dn",

25 *sum , * product , *sum - * product );

26 return 0;

27 }

when compiles the program with the clang compiler and then

runs it, he gets this output:

sum = 9

product = 6

sum - product = 6 - 6 = 0

And when he compiles the program with

the gcc compiler and then runs it, the program

terminates with a segmentation fault.

I see that one compiler is giving me an incorrect answer, and the other compiler is telling

me that Im using memory in an unsafe way but what am I doing wrong, and why does it

produce an incorrect answer?

What mistake did Archie make? Why does *sum have the value 6 on line 25? Why does

*sum - *product produce the value 0?

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

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions