[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

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);
}
0
guidway
Asked:
guidway
  • 5
  • 4
2 Solutions
 
Sjef BosmanGroupware ConsultantCommented:
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.
0
 
guidwayAuthor Commented:
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
0
 
guidwayAuthor Commented:
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);
}
0
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
avizitCommented:
exactly
in

 plPos = plPos + plLen;  
 you are changing the value of the pointer and NOt the value of the object pointed by the pointer

while in

*plPos = *plPos + plLen;

you do it correctly

to change the pointer value you would have to do as  sjef_bosman said , i.e pass long **pplPos

/abhijit/
0
 
Sjef BosmanGroupware ConsultantCommented:
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
...
}


0
 
guidwayAuthor Commented:
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.
0
 
Sjef BosmanGroupware ConsultantCommented:
lNext is a long, isn't it? You pass the address of lNext to the function. If you write
    *plPos = *plPos + plLen;
then you update the content of lNext. If that is what you want, splendid, but I assume you defined lNext as a long. Or how is lNext defined?

Example:
    lNext contains 12345
    the address of lNext= ABCDABCD
    assume plLen to be 4

    plPos= &lNext which means lpPos= ABCDABCD

now
    plPos = plPos + plLen effectively means plPos will be set to ABCDABCD+4, which is correct, since it will point to the data to be read next

whereas
    *plPos= *plPos + plLen would mean that lNext is set to 12345 + 4, since *ABCDABCD = 12345

What is lNext, how is it defined??
0
 
guidwayAuthor Commented:
correct, it is defined as a long and I do want to update the contents of lNext.
0
 
guidwayAuthor Commented:
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! :-)
0
 
Sjef BosmanGroupware ConsultantCommented:
Glad to have helped, and thanks for the points!

Sjef :)
0

Featured Post

Turn Raw Data into a Real Career

There’s a growing demand for qualified analysts who can make sense of Big Data. With an MS in Data Analytics, you can become the data mining, management, mapping, and munging expert that today’s leading corporations desperately need.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now