Quick question in C - THANKS FOR THE HELP

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
lets say I have a function void readFile(players *playerlist) , where players is a struct
if I want to call that function by reference, wouldn't I say readFile(&playerlist) ????
that's what I think I should be calling, but I'm getting this error:

Error : illegal implicit conversion from 'struct **' to 'struct *'
line 12 readFile(&playerlist);

 

AgentEL

Golden Member
Jun 25, 2001
1,327
0
0
It would be better to put this in the Software, programming forum.

anyways, it depends on how playerlist is delcared.

If it was

struct players playerlist; // then readFile(&playerlist) is correct

if

struct players * playerlist; // then readFile(playerlist) is correct

My guess is that you made it the second one. You're getting the pointer to a pointer by doing &(a pointer)
 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
AgentEL is right. Put the & in the prototype of the function. You're just sending the mem address of the pointer to the function.

-silver
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
I'm a little bit confused....

you say: struct players * playerlist; // then readFile(playerlist) is correct

that is what i did, but i don't think it passed by reference... after the function returns, the memory should be allocated for playerlist, but i'm debugging right now and the memory is still unallocated afterwards... which leads me to believe that this is still passing by value
 

Templeton

Senior member
Oct 9, 1999
467
0
0
passing a pointer into a function will not allocate memory for you, you'll either need to dynamically allocate space for playerlist then pass the pointer into the function, or statically declare playerlist and pass the reference into your function using &playerlist.

Edit:
This should work
struct players playerlist;
readFile(&playerlist);

It all depends on what you are trying to do though, you're situation may require you to dynamically allocate space using malloc.
 

cchen

Diamond Member
Oct 12, 1999
6,062
0
76
Originally posted by: Templeton
passing a pointer into a function will not allocate memory for you, you'll either need to dynamically allocate space for playerlist then pass the pointer into the function, or statically declare playerlist and pass the reference into your function using &playerlist.

Edit:
This should work
struct players playerlist;
readFile(&playerlist);

It all depends on what you are trying to do though, you're situation may require you to dynamically allocate space using malloc.

I obviously know that. what I meant is that I allocate space in the function, but when it returns from the function playerlist is still unallocated space for some reason
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Originally posted by: cchen
Originally posted by: Templeton
passing a pointer into a function will not allocate memory for you, you'll either need to dynamically allocate space for playerlist then pass the pointer into the function, or statically declare playerlist and pass the reference into your function using &playerlist.

Edit:
This should work
struct players playerlist;
readFile(&playerlist);

It all depends on what you are trying to do though, you're situation may require you to dynamically allocate space using malloc.

I obviously know that. what I meant is that I allocate space in the function, but when it returns from the function playerlist is still unallocated space for some reason

Within the readPlayer() fcn - do you at all call free(playerlist)? That would remove all memory from the playerlist pointer and the pointer would dangle. There is also the possibility that within the function you did something like a loop which reads a playerlist[] entry into a single local playerlist, which could mean you're returning the wrong value. Or possibly you somehow change the value of playerlist itself, meaning that you've changed the location of the pointer and it's not pointing to nothing.
 

manly

Lifer
Jan 25, 2000
13,479
4,177
136
Originally posted by: cchen
I'm a little bit confused....

you say: struct players * playerlist; // then readFile(playerlist) is correct

that is what i did, but i don't think it passed by reference... after the function returns, the memory should be allocated for playerlist, but i'm debugging right now and the memory is still unallocated afterwards... which leads me to believe that this is still passing by value
If I understand this right, the procedure readFile should be prototyped:
readFile(players **player_list);

IMO, AgentEL answered your original question correctly, but you asked the wrong question. If readFile is dynamically allocating memory, the call should look like:

*playerlist = (struct players *) malloc(LIST_SIZE * sizeof(struct players));

The reason is that the scopes are independent within the original caller and the callee (inside readFile). If you pass in a pointer and reassign the return value of malloc to that pointer, the allocated memory does not propogate back up to the caller. That leaves you with a memory leak, and the pointer in the caller is still undefined once you exit readFile.

Sigh, I didn't explain it as well as I'd have liked.

Is readFile a good procedure name for what it's worth?