Can someone post an example of how to use pointers?

I have been looking up pointers and what they do and I do not understand them at all. Can you post an example of a functioning program using pointers. It would be nice if it included comments explaining it. I know that you are not supposed to ask stuff like this, but I completely am missing the explanation of pointers, and I can't find an example that clearly explains it online.
They do exactly what they sound like. They point to data instead of actually representing it themselves. So a pointer to any type of data is the same size, because it just points to the first address of the block of data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int *a;
long int *b;
short *c; //All these pointers use up the same amount of space. They don't 
//actually store any data about floats or ints, they just point to an address

Some uses:

int *Array;
int x = 8;
Array = new int[x]; //Works even though x is non-constant


void func(int *Val)
{
*Val = 9;
}

int x = 0;
func(&x); //Will pass the address of x to func, which will change the value
//that's pointed to, or x. This is a way to change parameters outside the
//function without using the return value


int x[10] = {0};
int *p_x = &x[0];
p_x++; //p_x now points to x[1]. Pointer addition works like you would
//expect it to. 
So lines 13-19 make x 9 or Val 0?
I dont think pointer arithmetic is as straight forward to someone who doesn't know anything about it.

1
2
3
int x[10] = {0};
int *p_x = &x[0];
p_x++;


Take this for example. Let's just say the address of x[1] is 0x0002AF. If you increment this pointer, you are not just adding one to it, you are adding the size of the type the pointer is. So let's say an int is 4 bytes, if do p_x++ you are adding 4 to the pointer, which will actually point to the next element of the array. By writing something like, arr[7], you are essentially doing this. You are familiar with pointers, whether you realize it or not.

Seems as if the thing that gets people hung up on pointers is the various operators and symbols used with them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int apple = 12;  //Just makes an int, nothing special
int * ptr = &apple; //This makes a pointer that points to the address of apple. You have to initialize a pointer by giving it an address to point to before you can use it.

With pointers, you can directly change the value that the pointer is pointing to. This is useful in various situations

int banana = 12;
int * ptr;  //This is not initialized, have to fix this
ptr = &banana; //Now it is

//To change the value of whatever you are pointing to, you have to use the derefence operator, which is the asterick that you use to declare a pointer

*ptr = 15; //This just changed the value of banana to 15. Basically this says, "take that address you are pointing to, and actually change what is there"

A good rule of thumb when dealing with pointers, is given any expression, you can only have either a derefence operator OR a reference operator(&)


EDIT: Supplying a link
http://www.cplusplus.com/doc/tutorial/pointers/

And another... Moschops has a great article on all of this
http://www.cplusplus.com/articles/EN3hAqkS/
Last edited on
closed account (4z0M4iN6)
I will also try to explain.

Imagine, you have to arrays, maybe:

1
2
char a[size];
char b[size];


Now you want to copy array b to a:

for( int i = 0; i < size; ++i) a[i] = b[i];

Maybe such a copy would be a good idea and you would need it often, so you could wish to write a function, which does this.

But how could you write such a function? a[i] = b[i] you can't use, because
you don't want to do this only for this a and b.
And therefore we have pointers.
Pointers behave like arrays. But we can do, what we can't do with arrays.

We can assign an address of an array to a pointer:

1
2
char * destination = a; // array a
char * source = b; // array b 


and we could write the same then using the pointers:

for( int i = 0; i < size; ++i) destination[i] = source[i];

Because pointers are not a fix address, but a variable, we can assing other values, increase, decrease and all what we can do with variables.

The example above we could write also in this way:

1
2
3
4
5
6
for( int i = 0; i < size ; ++i )
{
     *destination = *source;
     destination++;
     source++;
} 


*destination could also be written as destination[0].

But consider after we have increased destination, destination[0] then means a[1] and so on


A short form of the example above would be:

for( int i = 0; i < size ; ++i ) *destination++ = *source++;

Because we don't like to write this every time - then we could also have taken the arrays - we could like to make a function:

1
2
3
4
void MyMemcopy( char * Destination, char * Source, size_t count)
{
    for( size_t i = 0; i < count ; ++i ) *destination++ = *source++;
}

And after this we could simply write instead our first copy of array b to a:

MyMemcopy( a , b , size);

You know the function memcpy, which you get, if you #include <memory.h>?
This function we have written now self.

But memcpy performs much faster, because it can use special processor registers for exact this purpose.

Do you understand now the purpose of pointers?
Last edited on
Thanks guys. I kind of get it now after running and modifying a few of your examples, but I still think it is going to b a while before I fully grasp the concept.
Topic archived. No new replies allowed.