1924 views|3 replies

11

Posts

0

Resources
The OP
 

About functions that return pointers [Copy link]

This post was last edited by oJingtiano on 2020-4-15 11:45

I am a beginner and I am currently learning the relationship between pointers and functions. I have written a function whose return value is a pointer.

The code is as follows:

typedef u8 *pu8;

uint8_t *sum(uint8_t value, uint8_t data)
{
pu8 sum;
uint8_t a = 0;
printf("value:%d, data:%d\n", value, data);
a = value + data + data;
sum = &a;
printf("&a:%p, a:%d, sum:%p, *sum:%d\n", &a, a, sum, *sum);
return sum;
}

main()

{

system_init();

while(1)

{

printf("sum:%p, *sum:%d \n", sum(2,3), *sum(2,3));

}

}

There is a separate printf function in while(1) that prints out at intervals. The currently set print content is the printf in while(1).

After I compiled it, I downloaded it to the microcontroller, and after running it, the serial port output was:

value:2, data:3
&a:17f5, a:8, sum:17f5,* sum:8

value:2, data:3
&a:17f5, a:8, sum:17f5, *sum:8
sum17f5, *sum:111

Why is the final result of *sum 111 instead of 8?

This post is from Programming Basics

Latest reply

sum points to a, but a is a non-existent variable after the function call (the memory previously occupied by a is reclaimed), so sum points to uncertain content (although the address has not changed, the content is no longer controllable). Fortunately, the returned result is not what you expected. If 8 is returned, it will lead you astray. In C, you should never return a pointer to a local variable.   Details Published on 2020-4-15 11:57
 

11

Posts

0

Resources
2
 
This post was last edited by oJingtiano on 2020-4-15 11:47

Ask for help

This post is from Programming Basics
 
 
 

6040

Posts

204

Resources
3
 

sum points to a, but a is a non-existent variable after the function call (the memory previously occupied by a is reclaimed), so sum points to uncertain content (although the address has not changed, the content is no longer controllable). Fortunately, the returned result is not what you expected. If 8 is returned, it will lead you astray. In C, you should never return a pointer to a local variable.

This post is from Programming Basics

Comments

Thank you for your help. I realized that it might be problem a, but after modifying it, I found that it was still 111, so I didn't think about it carefully. Now after rewriting it, the problem has been solved.  Details Published on 2020-4-15 13:40
 
 
 

11

Posts

0

Resources
4
 
lcofjp posted on 2020-4-15 11:57 sum points to a, but a is a non-existent variable after the function call (the memory previously occupied by a is recycled), so sum points to an uncertain memory...

Thank you for your help. I realized that it might be problem a, but after modifying it, I found that it was still 111, so I didn't think about it carefully. Now after rewriting it, the problem has been solved.

This post is from Programming Basics
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list