Link to home
Start Free TrialLog in
Avatar of guidway
guidwayFlag for United States of America

asked on

problem passing by reference

I'm having a problem passing by reference. I am trying to get lNext to have an updated value everytime the GetRec function is called and yet I must be doing something wrong because it appears to always have the value of 1 (its initial value). What am I doing wrong? thanks

call to the function:

lRecords = DecodeUSINT(GetRec(&lNext,4));

the function:

char *GetRec(long *plPos, long plLen)
{
   /*read part of file from pos for length*/
   char *sBuf;
   long pos;
   sBuf = (char*) malloc(sizeof(plLen));
   sprintf(sBuf,"%*s", plLen, " ");
   fread(sBuf,1,plLen,inFile);
   plPos = plPos + plLen;
   return sBuf;
   free (sBuf);
}
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

This will only work if you use the ADDRESS OF plPos as a parameter. In the function you modify plPos to point after the info returned, but the value of plPos is chucked away after the function. You need to pass a long **pplPos, so you can save the pointer in the calling function.
Avatar of guidway

ASKER

Hi sjef_bosman,

not quite sure I understand you... granted I haven't programmed in C in a few years now so bear with me, please... I thought &lNext inside my calling function was passing the address of lNext which is where the value is to be stored? lNext is a global variable by the way. plPos is strictly a local variable that should have the address of lNext, correct? Just wanting to make sure I understand this stuff still... ;)

guid
Avatar of guidway

ASKER

strange enough... this seems to have fixed it

*plPos = *plPos + plLen;

I don't understand why though...

char *GetRec(long *plPos, long plLen)
{
   /*read part of file from pos for length*/
   char *sBuf;
   long pos;
   sBuf = (char*) malloc(sizeof(plLen));
   sprintf(sBuf,"%*s", plLen, " ");
   fread(sBuf,1,plLen,inFile);
   *plPos = *plPos + plLen;
   return sBuf;
   free (sBuf);
}
SOLUTION
Avatar of avizit
avizit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This cannot have fixed it, you updated the long that plPos points to.

You need the value of plPos outside the function, don't you? plPos points to one long, and if you update plPos the changes to the pointer get lost when you leave the function, since it is a local var. The only 2 ways are: make it a static or global (yuck!) variable, but that won't work in this case, or update a pointer to plPos with the latest position of the pointer. Hence you need a pointer to a pointer to a long.

You should look into the following:

call to the function:

plNext= &lNext;
lRecords = DecodeUSINT(GetRec(&plNext,4));

the function:

char *GetRec(long **plPos, long plLen)
{
...
for you to find out
...
}


Avatar of guidway

ASKER

wow, upping the points because I am getting a good lesson in pointers right now. thank you for your help, both of you deserve extra points! :)

sjef_bosman,

>>You need the value of plPos outside the function, don't you?<<

Won't lNext contain the plPos value since I'm passing in the address of lNext? I understand that plPos is a local variable and will be destroyed once I leave the function but won't lNext still have that value from plPos?

sorry, I'm just trying to understand the reasoning still between both ways to see what is happening here. thanks for your patience.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of guidway

ASKER

correct, it is defined as a long and I do want to update the contents of lNext.
Avatar of guidway

ASKER

After reading your last post I think we were slightly thinking a little different on what I was looking for. My apologies if I did not explain it clearly before. I believe that answered my question though. thank you for your help! :-)
Glad to have helped, and thanks for the points!

Sjef :)