Solved

problem passing by reference

Posted on 2004-04-27
10
562 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
  • 5
  • 4
10 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 11

Assisted Solution

by:avizit
avizit earned 100 total points
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Author Comment

by:guidway
Comment Utility
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
Comment Utility
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
Comment Utility
correct, it is defined as a long and I do want to update the contents of lNext.
0
 
LVL 12

Author Comment

by:guidway
Comment Utility
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
Comment Utility
Glad to have helped, and thanks for the points!

Sjef :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now