Go Down

Topic: Legitimate uses for pointers? (Read 14148 times) previous topic - next topic

tylernt

Aug 15, 2013, 04:05 pm Last Edit: Aug 15, 2013, 04:09 pm by tylernt Reason: 1
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!

Code: [Select]

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

Code: [Select]

blue=modifyColor(blue);


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

AWOL

Quote
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.

Arrch

So what advantage is there to pointers?


Try to have the function modify three variables.

econjack

The code:

Code: [Select]

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.

tylernt

Ok, so,

Code: [Select]

blue=modifyColor(blue);

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


Or,

Code: [Select]

byte red;
byte green;
byte blue;

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

modifyColor(byte color)
 {
 color++;
 }


?

Arrch

#5
Aug 15, 2013, 04:22 pm Last Edit: Aug 15, 2013, 04:25 pm by Arrch Reason: 1

 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().

tylernt


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)

AWOL


radman

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

tylernt


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?

econjack

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:

Code: [Select]
modifyColor(red, green, blue);

How do you do that without pointers?

tylernt


Code: [Select]
modifyColor(red, green, blue);

How do you do that without pointers?
Make red, green, blue global variables.

Arrch


Make red, green, blue global variables.


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

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.

tylernt


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.

Go Up