Hey, I'm stuck when trying to make this code work, I get SIGSEGV, now I'm sure it is because I havn't defined the structures I'm trying to modify, but how am I supposed to define them?
Say in the function, if I have to define the structure, then I would need to pass the argument of the function as a double-pointer, thus changing the address of both the copy and the actual pointer, but that doesn't seem to work either. How can I work this out?
This is the code that passes the pointer as a copy, and SIGSEGVs:
Code:
typedef struct {
size_t id;
float mn;
char name[256];
} client_t;
void function(client_t *pclient);
int main(void) {
client_t *client;
printf("%p \n", client);
printf("%i \n", client->id);
printf("%f \n", client->mn);
printf("%s \n", client->name);
function(client);
printf("%p \n", client);
printf("%i \n", client->id);
printf("%f \n", client->mn);
printf("%s \n", client->name);
return EXIT_SUCCESS;
}
void function(client_t *pclient) {
pclient->id = 3;
pclient->mn = 35.43;
strcpy(pclient->name, "hola");
}
This is the code that passes it as a double pointer and has everything defined a priori, but when printing pclient for the second time it doesn't show the supposedly modified results.
Code:
int main(void) {
client_t *pclient;
client_t client;
pclient = &client;
printf("%p \n", pclient);
printf("%i \n", pclient->id);
printf("%f \n", pclient->mn);
printf("%s \n", pclient->name);
function(&pclient);
printf("%p \n", pclient);
printf("%i \n", pclient->id);
printf("%f \n", pclient->mn);
printf("%s \n", pclient->name);
return EXIT_SUCCESS;
}
void function(client_t **pclient) {
client_t client;
*pclient = &client;
(*pclient)->id = 3;
(*pclient)->mn = 35.43;
strcpy((*pclient)->name, "hola");
}
I'm not sure how to work with pointers, structures and functions at all, I mean I was doing fine with other data types (int, float, char, etc) when using pointers with functions but structures don't seem to be working out for me. I'm clueless.
-> The identation is OK, somethings off when c&p.