3
I have the following function
typedef struct userDataStruct
{
char name [MAX_NAME_LENGTH+1];
struct userDataStruct *next;
} userDataType;
errorType GetUsers (userDataType **list)
{
FILE *file;
char buffer [LINE_LENGTH+1];
userDataType *first, *previous, *new;
file = fopen ("abc.dat", "r");
first = previous = NULL;
while (fgets (buffer, LINE_LENGTH+1, file))
{
new = (userDataType *) malloc (sizeof (userDataType));
/* Resto do codigo */
if (first == NULL)
first = new;
if (previous != NULL)
previous->next = new;
previous = new;
}
list = &first;
return ok;
}
(ignore any syntax error, I had to dry up)
When I need to call this function, I declare a pointer and pass it with the null value. For example:
int main (void)
{
userDataType **usersList = NULL,
*currentUser;
GetUsers(usersList);
currentUser = *usersList; /* Aqui está o erro */
}
At the given line, the variable currentUser has the null value.
If I used the function malloc to allocate memory, this variable should not be pointing to the first element of the list?
Thank you in advance.
Do a [mcve], if you took out parts and it doesn’t work we may find that the problem is because of this.
– Maniero
The part I took just read the file, did not involve the problem in question.
– Gabriel Távora
There are other pieces missing, and exactly for missing and you saying no, we have no way of knowing if you have everything you need and then it becomes difficult to help properly.
– Maniero
I believe the problem is in the use of pointers, reading again I realized that I am changing the parameter of the first function (Getusers) and (I believe) this is not possible. But I don’t know how to do it properly.
– Gabriel Távora
The line where I do "parameter = &struct" does not have the expected effect.
– Gabriel Távora