Sign in to follow this  
DevFred

pointers

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Who is your target audience?

I've always found a (manual) linked list to be the best just-past-beginner example of pointers.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 stack

Vehicle* car = new Car(); //create on the heap
Motorbike 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 pointer
vehicles.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 heap
Motorbike 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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Showing how pointers are typically used in C++ is easy: they're not used, unless as a private non-aliasable implementation detail.

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this