'Initializing struct within a function then storing its address
Hi I have a struct like this
struct small_struct {
int a;
int b;
}
struct big_struct {
struct *small_struct child;
}
I want to pass the pointer of big_struct as a parameter into a function in which child is initialized.
static int my_function(struct big_struct* s) {
if (certain_condition)
s->child = &(struct small_struct) {
.a = 1;
.b = 2;
}
}
However, when I do this and my_function is finished, the fields in s->child are often changed outside of the my_function. Would there be a way to keep a and b values as it was initialized inside my_function?
Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
