Solved

problem passing by reference

Posted on 2004-04-27
10
567 Views
Last Modified: 2010-04-02
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
Comment
Question by:guidway
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10929478
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
 
LVL 12

Author Comment

by:guidway
ID: 10929576
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
 
LVL 12

Author Comment

by:guidway
ID: 10929619
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:avizit
avizit earned 100 total points
ID: 10929694
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10929760
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
 
LVL 12

Author Comment

by:guidway
ID: 10930093
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
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 300 total points
ID: 10930259
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
 
LVL 12

Author Comment

by:guidway
ID: 10930301
correct, it is defined as a long and I do want to update the contents of lNext.
0
 
LVL 12

Author Comment

by:guidway
ID: 10930352
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10936586
Glad to have helped, and thanks for the points!

Sjef :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Assigning default value to structure in C for mutithread application 17 75
Please explain C language error codes ? 5 42
windows 10 pro lost profile. 10 65
Reactjs with .NET 3 70
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question