1

I'm maintaining some C code that was written by the person who had my job last. A situation has come up regarding passing by reference. Here's a shortened, contrived example of what I'm working with:

static int b;

void SetToTen(int *a){
    b = 10;
    /* >>>>>>>  Need to set a equal to b on this line <<<<<<<< */
return;
}

int main{
    int a = 0;
    SetToTen(&a);
    /* Now a should be equal to 10*/
  . 
  .
  .
return 0;
}

In the SetToTen function, I could either write:

*a = b;

OR

a = &b;

I think these two are functionally equivalent (a will be equal to ten with either of them.) But my question is: are there any sneaky subtleties associated with one over the other? Specifically, if I use a = &b does that mean that if I change b in the future, a will change as well? And is this not the case if I use *a = b?

Thoughts/Musings/Comments would be appreciated.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
Ben
  • 13
  • 2

2 Answers2

5

They're not functionally equivalent at all. This code:

*a = b;

dereferences a and sets its value to that of b. This code:

a = &b;

changes the a pointer to point to b. If you use the second piece of code, nothing will happen because you're changing the a pointer by value, it won't actually change a at all.

wrren
  • 1,281
  • 9
  • 11
2
*a = b;

That's the correct one.

a = &b;

That's just copying an address into a value. The C language passes all arguments by value. (C++ has the notion of references) If you want to modify an argument passed into a function, you need to pass in an address or an array, and modify the value the address or array member refers to.

It might be better to declare and define the function as follows:

void setToTen(int *pa){
    *pa = 10;
}

with these changes from yours:

  1. pa (pointer-to-a) avoids confusion of using a as a value in your main function and a as a pointer in SetToTen()
  2. There's no real reason to use b anywhere, if the goal is to set the variable whose address is passed in to 10.
  3. Style/syntax: most C coding conventions use symbols starting with lowercase as variables/methods, and symbols starting with uppercase as types.
  4. Style/syntax: an explicit return statement is not necessary unless you are trying to return a value (your function is declared void) or if you wish to return prematurely before the end of the function.

Analogy: There are two houses: Mr. Smith's at 115 Main St., which is blue, and Mr. Jones at 205 Elm St., which is green. You have a slip of paper containing Mr. Smith's address.

*a = b; is like painting the house at the address on the slip of paper the same color as Mr. Jones's house.

a = &b; is like changing the address on the slip of paper to be Mr. Jones's address; it doesn't actually do anything of consequence.

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • 1
    +1 Everyone repeat after me now: "There is no 'passing by reference' in C". – David Gelhar Dec 07 '11 at 17:03
  • Sir, I saw your posy about good and bad about StackOverflow. I just want to share a question with you asking your opinion does that question deserved so many downvotes. https://stackoverflow.com/questions/34826036/explain-pointer-dereferencing . I am new here, but now I cannot ask any questions because this question was downvoted, but I do not see any reason. Could you please check. I asked you because I loved your post and thought you could help me out. – Suraj Jain Oct 02 '16 at 14:33
  • To be honest, the question isn't very well-written, and you answered it yourself after saying "I am a beginner in C" so it looks like you only posted to gain reputation points on this site. I would echo underscore_d's comment: "There are many questions and answers on this site and the wider internet about aliasing, pointer conversions, etc. There is no point in repeating all of those here." In your case I think you need to work at learning to post a clear and concise question or answer. If you can't post questions because of reputation, contact the moderators to see if they can fix it. – Jason S Oct 02 '16 at 16:35
  • No sir , I answered not because of this , the question I asked was different , and when no answer came , I tried searching elsewhere and when i found the answer I posted it. Answer got +3. But the question gained many downvotes , i then changed the question and answer. I asked question for the purpose to learn nothing else , but here as soon as a newcomer ask question moderator start running towards him to take down the question. I will reframe my question. Is my answer right ? – Suraj Jain Oct 03 '16 at 03:10