I'm able to assign values with strdup and print the values with this: (*test + index)->email but I have no idea how to free the memory allocated to the variable email. I thought about freeing test+index but I guess this would cause a memory leak, right? Taking into account that the struct has allocated memory and each of the pointer inside it have memory allocated with strdup.
Edit:
The code is roughly like this:
struct random {
char *email;
} Random;
void function(Random **struct) {
char *temp = calloc(100, sizeof(char));
*struct = calloc(5, sizeof(Random));
for (int i = 0; i < 5; i++) {
scanf("%s", temp);
(*struct + i)->email = strdup(temp); //This works
}
free((*struct + 3)->email); //Gives segmentation fault
}
int main() {
Random *struct;
function(&struct)
}
scanfinto uninitializedtempis going to be undefined behavior.tempneeds to bemalloced, or made into an array, not just achar*. Also, you use*struct + ihere, which makes no sense, given there is noivariable, nor arandomSizevariable.i? you are going to have to put in enough code to repro the issuestructas a variable name is not allowed.