Should we pass structure or pointer to structure? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Should we pass structure or pointer to structure?

if we have two function dist(struct point pt1, struct point pt2) dist (struct point *pt1, struct point *pt2) should we use 1st one or 2nd one? what is the difference we ll encounter while using both simultaneously?

10th Apr 2018, 6:10 AM
Aman Kumar Jain
Aman Kumar Jain - avatar
1 Answer
+ 2
"For small structs (eg point, rect) passing by value is perfectly acceptable. But, apart from speed, there is one other reason why you should be careful passing/returning large structs by value: Stack space. A lot of C programming is for embedded systems, where memory is at a premium, and stack sizes may be measured in KB or even Bytes... If you're passing or returning structs by value, copies of those structs will get placed on the stack, potentially causing the situation that this site is named after... If I see an application that seems to have excessive stack usage, structs passed by value is one of the things I look for first." Source: https://stackoverflow.com/questions/161788/are-there-any-downsides-to-passing-structs-by-value-in-c-rather-than-passing-a?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
10th Apr 2018, 9:25 AM
Emma