The program works fine when I dynamically allocated memory in "main" function. Now I want to allocate in "read" function, but every attempt I fail miserably.
I think that my problem is in my "main" function: I can't figure out how to retrive a structure (pointer) from function "read" and then free it's dynamically allocated memory by function "destroy".
int main(void)
{
int err_code;
struct student_t** s=(struct student_t**)malloc(1024*sizeof(struct student_t*));
**s = &read(err_code); //here is: error: lvalue required as unary '&' operand.
//But I think that my problem is wider than just this error.
if (s==NULL) {
puts("Error\n");
}
display(s);
destroy(s);
return err_code;
}
What I tried to do: create a pointer of struct type, pointing to pointer to the structure, returned by "read" function. Then pass this **pointer to "destroy" function, to free the malloc'ed memory.
Functions.
In function "read" users inserts data that is assigned to a structure. Returns pointer to the dynamically allocated structure or NULL if there was any error.
struct student_t* read(int *err_code)
{ printf("Insert data:\n");
struct student_t* p = (struct student_t *)malloc(1024*sizeof(struct student_t));
*err_code=1;
if (p==NULL) {
puts("Error\n");
return NULL;
}
//then it's supposed to read from user and assing to struct. Code below in links.
}
struct student_t {
char name[20];
char surname[40];
int index;
};
Fuction frees dynamically allocated memory, unless "read" failed and returned NULL.
void destroy(struct student_t **s)
{
if (s!=NULL) free(s);
}
My display function. But my problems begin earlier, I think.
void display(const struct student_t **s) //here I'm unsure if it should be *s- or **s-function.
{
if(s!=NULL) printf("%s %s, %i\n", (*s)->name, (*s)->surname, (*s)->index);
}
My "read" function is based on answers for my previous questions. It works when I properly allocated memory in "main". The code for "read" I use: How to detect if user inserts data with commas (in a desired format) or not?
Other simplier "read" with which I was unable to handle all errors I wanted properly: How to scanf commas, but with commas not assigned to a structure? C
I really appreciate all help, everything is like a salvation for my 150 hours struggle with one task.