Legitimate uses for pointers?

I've always read that "real C programmers use pointers" but as of yet, never had a good reason to use them. Then, last night I thought I had an epiphany. I had a function that had to be able to modify one of three variables. Ah, says I, I finally found a reason to use pointers!

setup()
  {
  byte red;
  byte green;
  byte blue;
  modifyColor(&red);
  modifyColor(&green);
  modifyColor(&blue);
  }

void modifyColor(byte *color)
  {
  *color++;
  }

But then this morning I realized I could just do

blue=modifyColor(blue);

and accomplish the same thing without a pointer. So what advantage is there to pointers?

But then this morning I realized I could just do

Code:

blue=modifyColor(blue);

and accomplish the same thing without a pointer

Not with that void function you can't.

tylernt:
So what advantage is there to pointers?

Try to have the function modify three variables.

The code:

blue=modifyColor(blue);

works fine, but you'll need two other assignment statements for the other two colors. If you use a pointer, modifyColor() does need to concern itself which color is changed since no return value is needed and the return type can remain void since no assignment statement is needed. Write your code for changing all three colors, one version using pointer and the other without pointers and tell us about the code size.

Ok, so,

blue=modifyColor(blue);

byte modifyColor(byte color)
  {
  color++;
  }

Or,

byte red;
byte green;
byte blue;

setup()
  {
  modifyColor(red);
  modifyColor(green);
  modifyColor(blue);
  }

modifyColor(byte color)
  {
  color++;
  }

?

tylernt:
modifyColor(red);
modifyColor(green);
modifyColor(blue);

modifyColor(byte color)
{
color++;
}

Nope. The return values have to be assigned and functions need a return type.

Let me rephrase: try having it modify three colors with a single call to modifyColor().

econjack:
tell us about the code size.

Using global variables: Binary sketch size: 466 bytes (of a 32,256 byte maximum)
Using return value assignment: Binary sketch size: 466 bytes (of a 32,256 byte maximum
Using pointers: Binary sketch size: 466 bytes (of a 32,256 byte maximum)

Or use references...

You can use pointers to directly address hardware e.g. registers in a UART.

Arrch:
Let me rephrase: try having it modify three colors with a single call to modifyColor().

Hm. How would I do that with pointers? I'd have to pass modifyColor(&red, &green, &blue) and deal with each argument inside modifyColor, no?

Arrch makes a better point. (Indeed, you can write the pointer and non-pointer code and get the same code size...pretty good compiler.) What Arrch is asking is, what if you want to increment all three colors with one function call:

modifyColor(red, green, blue);

How do you do that without pointers?

econjack:

modifyColor(red, green, blue);

How do you do that without pointers?

Make red, green, blue global variables.

tylernt:
Make red, green, blue global variables.

Which defeats the purpose of having the function in the first place.

You can pass pointers to functions as arguments to functions. I admit I have not used that facility a lot but it can be useful.

Arrch:
Which defeats the purpose of having the function in the first place.

I guess I still don't understand the benefit of using global variables and modifyColor(red, green, blue) over calling modifyColor three times, once for each color, assigning the return values.

You can pass pointers to functions as arguments to functions. I admit I have not used that facility a lot

Really?

Serial.println ("The quick brown fox");

Without pointers, that one's really tedious.

There are kids in primary school that can't see the use of mathematics. I served in the Army with more than few that by their abilities stopped learning math once they could get change for a ten and hated anyone that went one step further. Show one algebra and they get all wound up about letters where numbers are all anyone needs, then challenge you to a fight to prove who's right.

Explanations would be too long and have already been typed. Pointers are not a small topic to become competent in quickly. Google on "C C++ pointer tutorial" (without the quotes).

Have a look through the standard libraries for commands that return pointers. If you do much with arrays then you should see uses for pointers right there.

But forum posts are no substitute for reading from books or sites and practicing what is shown there. If that's not enough then you need a teacher until you learn to "dress and feed" yourself.

Just wait till you learn about C++ class objects.

radman:
You can pass pointers to functions as arguments to functions. I admit I have not used that facility a lot but it can be useful.

Can we do that on a Harvard architecture Arduino with a separate address space for code?

What about structures; In C, how would you conveniently process those if you could not pass pointers to them as arguments?

tylernt:

radman:
You can pass pointers to functions as arguments to functions. I admit I have not used that facility a lot but it can be useful.

Can we do that on a Harvard architecture Arduino with a separate address space for code?

Yes.