Question about using pointer reference &

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 C Programming/Pointers and arrays - Wikibooks, open books for an open world and just got more confused.

I think this holds a truth:

C is known for giving you just enough rope to hang yourself

So thank you in advance.

My code:

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

1
2
3
4
5
etc.

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:

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;
}

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

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 :slight_smile:

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

[quote author=Nick Gammon link=topic=149001.msg1119467#msg1119467 date=1360876831]
I just didn't like "this_is_not_i" but I presume you don't normally do that. :)[/quote]
Haha, thought so :slight_smile: Yeah, don't normally do that, but when one is just testing one thing in coffeine high, things might get out of hand...