Go Down

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

AWOL

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

Really?
Code: [Select]
Serial.println ("The quick brown fox");
Without pointers, that one's really tedious.

GoForSmoke

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.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

tylernt


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?

radman

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

GoForSmoke



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.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

radman

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

Code: [Select]
/* function returning pointer to int */
int *func(int a, float b);

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

tylernt


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.

Arrch


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?

wildbill

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.

tylernt


Where did you hear that C couldn't return strings or structs from a function?
Let me rephrase that, C's inability to return a string or struct without a pointer. At least, the code examples I see on websites look like

Code: [Select]
char* functionName(arguments)

I see a pointer "*" in there.

AWOL

Quote
Let me rephrase that, C's inability to return a string or struct without a pointer

No, structs are returnable without pointers.

radman


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


It is possible to write recursive code in languages that don't have inherent support for it e.g. FORTRAN, however it is a pain.
Trying to solve a problem that is naturally recursive without using recursion would I think be really difficult.
For example how would you solve the Eight Queens problem (where you place 8 queens on a chessboard with no queen attacking another), without using recursion?

Whilst a lot of things can be done without pointers avoiding using them can make life much harder.

Arrch



Where did you hear that C couldn't return strings or structs from a function?
Let me rephrase that, C's inability to return a string or struct without a pointer. At least, the code examples I see on websites look like

Code: [Select]
char* functionName(arguments)

I see a pointer "*" in there.


That's because a string IS a pointer. More specifically, it's an array of chars that is null-terminated, and an array is represented by a pointer to its first element. The reason it's done like this is so that strings can have variable sizes. Try creating a variable type that doesn't have a fixed size without using pointers.

tylernt


No, structs are returnable without pointers.
So, one less reason to use pointers.

Quote
That's because a string IS a pointer. More specifically, it's an array of chars that is null-terminated, and an array is represented by a pointer to its first element

Ok... that makes it sound like there's no need for a C programmer to explicitly use pointers (i.e., using the & or * prefix), because the pointer implicit in a string is all you need to get the job done.

Quote
Whilst a lot of things can be done without pointers avoiding using them can make life much harder.
Can you give me an example please? Assignment by return value doesn't seem to be any harder (other than typing a few more characters and an = sign) than using an explicit (& or *) pointer.

Sorry if this comes across as inflammatory... I'm really trying to learn here. I'm a bit dense but I want to "get it".

GoForSmoke

What you will find in tutorials is usually contrived examples just to keep things simple. It's when a use fits in an app you're working on that you will come to appreciate them or if you never learned them you can hate them instead.

The same goes with char array strings and string.h commands. If all you know is C++ String objects then you can argue against the C strings you don't know. I'll leave it on that, it's a can of worms in itself.

Pointers are good tools in a lot of places though they do use a word of memory.

Recursion is great if you have the RAM to go as deep as deep may get. There's ways around it just as there is with pointers but generally it's longer, harder, less flexible especially when trying to make general case functions.

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

Go Up