I am trying to write a separate program that calls a function to dynamically allocate memory for a certain number of "student" structs.
My main program was too large to mess with so I created a smaller program to help me more easily figure out what I'm doing:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "memtest.h"
void structMemInit(myName **);
int main(void){
myName *myNameIs;
structMemInit(&myNameIs);
printf("%s\n", myNameIs[1].name);
return 0;
}
void structMemInit(myName **myNameIs){
*myNameIs = (myName *) calloc(5, sizeof(myName));
if(myNameIs == NULL){
printf("allocating memory didn't work!\n");
exit(1);
}
else if(myNameIs != NULL)
(*myNameIs)[1].name = "Zach";
}
the memtest.h file is:
typedef struct{
char *name;
}myName;
All the above program is trying to do is pass a pointer to a struct into the function "structMemInit" and allocate clean space for the struct. Then to see show that it worked, I am setting the variable char to a name. After all this happens, you leave the function and go back to main. Then I print the name in the struct to show it worked.
When run, the program always gives a segfault.
In case you are wondering why I have a separate .h file, my actual program is much larger and has several globally declared structs and I am required by my instructor to keep a separate .h for those structs.
Thanks! Zach
Please note that:
is different from:
In the second form, you are not allocating any space for the string. You are just allocating space for a pointer.
Below is a running version of your program. If you want to use
strcpy(p->name, "Zach");
you need to usechar name[20];
in your struct.Before I answer, I should state that I am not too well-versed in C, I am a C++ programmer so there may be a few grammatical errors in what follows. Hopefully though, my point will be made sufficiently for any errors to be of much importance.
The reason you're getting segfault is because of your misunderstanding of what's happening when you pass a pointer as an argument to a function.
Imagine, for instance, your program was the following:
In this case, you can clearly see that the output will be undefined because the variable
i
was not initialised, and was passed by-value tosetInteger
. Any changes ton
are not transferred back toi
when the function returns.If the program were replaced with
Then the value output by
i
will be12
.The same thing is happening in your program, you're passing your pointer by value.
If you want to pass by reference, you need to do the following:
That's, unfortunately, not the end of your problems. Although you've allocated memory to your
myName
object, you have not allocated memory to your character array.To return the string "Zach", you need to do the following:
If you insist on passing the pointer, it should be by reference, so it's a pointer to a pointer, as the function itslef will modify the address in the pointer.
You're much better off if your function returns a pointer.
BTW it's
int main(void)
notint main()
.