Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old July 3rd, 2008, 10:38 AM
Member
 
Join Date: Aug 2007
Posts: 34
Default Simple pass-by-reference question

How do I get the pass by reference-thing to work in this example?
Since I'm still new to C I sometimes get confused in this matter, and this is one of those occasions.

I'm obviously doing something wrong.
Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3. char *params =  mem_alloc(sizeof(char)*64);
  4. getParams(params);
  5. printf("Parameters: %s",params);
  6. mem_free(params);
  7. }
  8.  
  9. int getParams(char *p)
  10. {
  11.   p = "something, something";
  12.   return 0;
  13. }
what I want is of course the result to be "Parameters: something, something".
Thanks!
Reply
  #2  
Old July 3rd, 2008, 10:53 AM
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 8,491
Default

There doesn't exist a call by reference parameter passing mechanism in C; it only
uses call by value for passing parameters around. You can still copy your string
to that chunk of memory. Change your line 11 as follows:

Expand|Select|Wrap|Line Numbers
  1. strcpy(p, "something, something");
  2.  
kind regards,

Jos
Reply
  #3  
Old July 3rd, 2008, 11:23 AM
Member
 
Join Date: Aug 2007
Posts: 34
Default

Ok, thank you very much, my problem is solved!
However, I'm trying to really understand why just writing
Expand|Select|Wrap|Line Numbers
  1. p="something,something";
  2.  
doesn't work. While debugging, I see that the params-pointer and the p-pointer both point to the same address. That's why I'm confused.
But is it because the pointer p only gets assigned to point to the string 'locally', so once we're outside the scope of the getParam - method, this assignment is all forgotten about? If that's the case, I think I understand. Anyhow, again thank you so very much!
Reply
  #4  
Old July 3rd, 2008, 12:15 PM
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 8,491
Default

Quote:
Originally Posted by MimiMi
But is it because the pointer p only gets assigned to point to the string 'locally', so once we're outside the scope of the getParam - method, this assignment is all forgotten about? If that's the case, I think I understand. Anyhow, again thank you so very much!
Yep, that's how call by value works: just values are passed to a function and
assigned to the parameters as if they were local variables. If you change them
you don't change anything outside that function. If such a value happens to be
an address and you change some memory content at that address, the changes
will be permanent then, i.e. not local to that function.

kind regards,

Jos
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles