Connecting Tech Pros Worldwide Help | Site Map

Simple pass-by-reference question

  #1  
Old July 3rd, 2008, 10:38 AM
Member
 
Join Date: Aug 2007
Posts: 42
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!
  #2  
Old July 3rd, 2008, 10:53 AM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Simple pass-by-reference question


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
  #3  
Old July 3rd, 2008, 11:23 AM
Member
 
Join Date: Aug 2007
Posts: 42

re: Simple pass-by-reference question


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!
  #4  
Old July 3rd, 2008, 12:15 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Simple pass-by-reference question


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL PassThrough Question lukethegooner answers 3 November 17th, 2008 05:55 AM
Time out question DarkBlue answers 6 July 4th, 2006 10:25 AM
How to pass STL containers (say a vector) ? Sanjay Kumar answers 23 May 22nd, 2006 11:35 PM
Another simple question - passing variables Brad answers 3 November 19th, 2005 08:47 AM
How can I pass an image parameter in HTML? Is it possible??? John answers 23 July 24th, 2005 12:51 AM