Go Down

Topic: Question about using pointer reference & (Read 335 times) previous topic - next topic

Jaakko_Fagerlund

Hello

I've coded before with C++ on computers and have learned some things in a course once, though then they didn't teach to the 'whys' so much. This is why I'm not sure of some things and don't know the specifics behind some things.

My question is of using the reference (&) in a functions parameter list. The code below gives out an increasing reading so it works, but is this coding bad practice, okay to use, totally not how to do it and why so? I'm especially interested in the why part, like is this style hogging memory if I use it extensively or if it isn't suitable for some things.

I read the C programming Wikibook entry at http://en.wikibooks.org/wiki/C_Programming/Pointers_and_arrays and just got more confused.

I think this holds a truth:
Quote
C is known for giving you just enough rope to hang yourself


So thank you in advance.

My code:

Code: [Select]
void setup()
{
  Serial.begin(9600);
}

unsigned long i = 0;

void loop()
{
  increase(i);
  Serial.println(i);
  delay(500);
}

void increase(unsigned long &this_is_not_i)
{
  this_is_not_i++;
  return;
}


Result in serial monitor
Code: [Select]

1
2
3
4
5
etc.

Nick Gammon

What you are doing is fine (except for the data names).

A reference is like a pointer. When you call "increase" a reference (pointer to, in effect) to its argument is passed, so that the function receives, and can modify, its argument. It's actually quite efficient, particularly if you are passing something big, because it doesn't have to make a copy, it just passes the address (reference) of the thing.

I prefer references to pointers, but there are times when a pointer is more suitable, for example in an array, because references cannot, in general, be changed at run-time.

Functionally equivalent would be this, and you can see it is messier because of the extra punctuation needed:

Code: [Select]

void setup()
{
  Serial.begin(9600);
}

unsigned long i = 0;

void loop()
{
  increase(&i);
  Serial.println(i);
  delay(500);
}

void increase(unsigned long * pointer_to_i)
{
  (*pointer_to_i)++;
  return;
}
http://www.gammon.com.au/electronics

Jaakko_Fagerlund

Thank you for your response, very informative and easy to understand :)

Seems that I've been doing the right thing, though not knowing it for sure.

About the variable names, could I ask what specifically was wrong with them and what would be the proper notation? I know they are not to some conventions and I just slapped them in a hurry to test things, normally I use caps in CONSTANTS and descriptiveVariableNames and name_my_functions so they are easy to distinguish from anothers. Maybe not right, but makes reading easier :)

Nick Gammon

I just didn't like "this_is_not_i" but I presume you don't normally do that. :)
http://www.gammon.com.au/electronics

Jaakko_Fagerlund


I just didn't like "this_is_not_i" but I presume you don't normally do that. :)

Haha, thought so :) Yeah, don't normally do that, but when one is just testing one thing in coffeine high, things might get out of hand...

Go Up