Legitimate uses for pointers?

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.

I was meaning have not used pointers to functions a lot as in the second below;

/* function returning pointer to int */
int *func(int a, float b);

/* pointer to function returning int */
int (*func)(int a, float b);

GoForSmoke:
But forum posts are no substitute for reading from books or sites and practicing what is shown there.

That's just it... I've been reading C books and sites for years now. I guess I haven't been reading the right books and sites since it seems like for every book or site where a pointer is used, it's not the only way to skin that particular cat.

Don't think I'm hating on C or pointers because I don't understand them. But it's looking to me like pointers are a lot like recursion (if you're smart enough to do recursion, then by definition you're smart enough to find a way to do the same thing without recursion).

I'll admit using pointers for function calls dealing with strings and structures appear to be a good use of pointers, but it seems to me that's mainly just a band-aid for C's inability to return a string or struct from a function.

tylernt:
but it seems to me that's mainly just a band-aid for C's inability to return a string or struct from a function.

Where did you hear that C couldn't return strings or structs from a function?

Some data structures are easier to implement with pointers. Consider linked lists & trees, or indeed anything that uses dynamic memory allocation. Not that you'd really want to use those things in an Arduino context with so little RAM.