Pages: [1]   Go Down
Author Topic: Question about using pointer reference &  (Read 241 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
1
2
3
4
5
etc.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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 smiley
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just didn't like "this_is_not_i" but I presume you don't normally do that. smiley
Haha, thought so smiley Yeah, don't normally do that, but when one is just testing one thing in coffeine high, things might get out of hand...
Logged

Pages: [1]   Go Up
Jump to: