This is a basic question, but I can't seem to understand how this works:
Suppose I pass a structure pointer to a function, do I need to malloc memory for this passed pointer inside the function?
e.g.,
typedef struct example_data_ {
int serial;
char *name;
} example_data;
int main() {
int ret;
example_data *ptr_ex;
// Should I malloc memory for ptr_ex here before assigning values to it???
ptr_ex->serial = 1;
ptr_ex->name = 'test';
myfunc(ptr_ex);
return 0;
}
void myfunc(example_data *myptr) {
//Can I directly deference the values below??? or should I malloc memory for *myptr also???
printf("serial no: %d\n",myptr->serial);
printf("name: %s",myptr->name);
}
Thanks !