pointers

Started by
7 comments, last by Codarki 14 years, 11 months ago
I just had to come up with a short, "real world" example of when and how pointers are used. What I don't like at all are examples like these:

int i = 4;
int* p = &i
*p = 2;
// i is now 2
So question #1 is: What do you think is a good, SHORT example (fits on one slide) that demonstrates the use of pointers? My answer is... finding a character in a string :) Question #2: How would you implement that, so that the program demonstrates important features of pointers? My answer is...
// SPOILER ALERT
// scroll down to read my solution
// ...
// ...
// ...
// ...
// ...
// ...
// ...
// ...
// ...
// ...
// ...
// ...
// ...
// ...
// ...

int index_of(char c, char* string)
{
    for (char* p = string; *p; ++p)
        if (*p == c)
            return p - string;
    return -1;
}

Yes I know, it should be const char* instead of char*, but that's not the point here. I like this version because it shows pointer arithmetic with ++p and p - string, but what I miss is the usage of the address-of operator &. So, can you come up with other interesting examples?
Advertisement
The way boost::shared_ptr does reference counting is a pretty quick bit of code and illustrates a case where you might choose to use a pointer instead of a reference. If this is an in class example, I'd strip the template code out and illustrate it using a concrete type first.
Your example looks interesting, but, as a learner of C++, I require an explanation of the code.

Can you put your example to use, and explain to me the use of pointers? I do not fully understand what your code does.
Who is your target audience?

I've always found a (manual) linked list to be the best just-past-beginner example of pointers.
Quote:Original post by Telastyn
Who is your target audience?

Second semester Java students. The point of the code is to show a language feature that they don't know yet (pointers) and demonstrate how it is typically used.

This shouldn't take more than five minutes. They are not required to understand pointers in depth.
Another (hopefully understandable [smile]) example:
class Vehicle {...}class Car : public Vehicle{...}class Motorbike : public Vehicle{...}std::list<Vehicle*> vehicles; //default constructor is called, vehicles is on the stackVehicle* car = new Car(); //create on the heapMotorbike motorbike; //create on the stack (note that at the end of the block motorbike will be automatically destroyed)vehicles.add(car); //car is already a pointervehicles.add(&motorbike); //here the address-of operator is needed to get a pointer to motorbike


The same in java (1.5+):
class Vehicle {...}class Car extends Vehicle{...}class Motorbike extends Vehicle{...}List<Vehicle> vehicles = new ArrayList<Vehicle>();Vehicle car = new Car(); //create on the heapMotorbike motorbike = new Motorbike; //not the same here, since objects can't be created on the stack in java (you kind of always have a pointer)vehicles.add(car);vehicles.add(motorbike);

Using pointers in the C++ part is needed, since you can't stuff values of different sizes into a list - and Car and Motorbike would be truncated/"degraded" to Vehicle if you did use std::list<Vehicle>.

Edit: some fixes to the syntax [grin]

[Edited by - Lord_Evil on May 4, 2009 4:54:09 PM]
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Quote:Original post by DevFred
Quote:Original post by Telastyn
Who is your target audience?

Second semester Java students. The point of the code is to show a language feature that they don't know yet (pointers) and demonstrate how it is typically used.
Well, if we're talking about non-beginners coming from another language having to grok the intricacies of pointers in C then perhaps you might consider something along these lines:
struct string {	char letter;	struct string *next;};struct string *read_string(void) {	int letter;	struct string *head;	struct string **tail = &head	while(isgraph(letter = getchar())) {		struct string *curr = malloc(sizeof *curr);		curr->letter = letter;		*tail = curr;		tail = &curr->next;	}	*tail = NULL;	return head;}
I should think the pointer-to-pointer use here in particular ought to be immediately appreciated by a Java audience.
Showing how pointers are typically used in C++ is easy: they're not used, unless as a private non-aliasable implementation detail.
OP didn't mention if this was C or C++.

In C they are used extensively, and I agree string operations might be good example.

In C++ pointers are mostly used at dynamic allocation, and directly passed to smart pointers. Iterators are used for iterating, searching and pointing to containers and streams are used for i/o operations.

Whatever you do, please don't show C code and say it's C++.

This topic is closed to new replies.

Advertisement