Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 18th, 2004, 10:54 AM   #1
Ashcroft
Programmer
 
Join Date: Sep 2004
Posts: 38
Rep Power: 0 Ashcroft is on a distinguished road
This tutorial covers the very basics of pointers, really basic stuff - if it is insulting or overly pedantic or confusing please let me know.

People coming to C from other programming languages are often confused by pointers. I'm not going to claim it is the most intuitive thing in the world, but if you actually just play with some code and work through some examples it begins to make sense - really.

C has very few 'primitive' types.

char c;
int i;
float f;

c = 'A';
i = 42;
f = 3.14159;

c is type char, i is type int, f is type float - pretty straightforward.

A pointer is kind of like an additional primitive type that is specified with '*'.
char *pc;
int *pi;
float *pf;

pc is type pointer to char, pi a type pointer to int, and pf a type pointer to float. The '*' is part of the type and not part of the name. Many people would write these as 'char* pc' rather than 'char *pc' - whichever you prefer is the correct way to do it.

So the type of the variables are 'pointer to ...', now what do we assign to those variables?

pc = &c;
pi = &i;
pf = &f;

The '&' is the address of operator. That means that pi (pointer to int) holds the address of the variable i.

You may have seen that '&' operator before in a tutorial. The following is a bit of tutorial code I remember when I learned C.

#include <stdio.h>

int
main(int argc, char **argv)
{
    int n;
    printf("enter a digit to continue: ");
    scanf("%d", &n);
    printf("you entered %d\n", n);
    return 0;
}

You see in the argument to scanf we passed in the address of n rather than the variable itself. The reason for this will not make sense right now, I just wanted to let you know that you may have been using pointers already without even knowing it.

So how else can a pointer be used? The '*' is part of the type of a pointer variable but '*' also serves as the indirection operator. If you remember back to our earlier examples 'int *' is the type of the variable pi, if you print the value of pi itself you will have the address of whatever it points to, but if you print the value of '*pi' then it will peek at what pi points to and that is what gets printed.

#include <stdio.h>

int
main(int argc, char **argv)
{
    int i = 100;
    int n = 200;
    int *pi = &i;
    /* %x is a printf format arg to print values in hexadecimal */
    printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
      i, n, &i, &n, pi, *pi);
    return 0;
}

On my system that code prints the following:
i = 100, n = 200, &i = bffffcc0, &n = bffffcc4, pi = bffffcc0, *pi = 100

So what does that mean exactly? The 'i = 100 and n =200' should be pretty obvious, the &i and &n are a bit more mysterious - what the program prints is the memory address at which the variable i and n are actually located (I use %x to print the memory addresses because it is traditional to print memory addresses in hex ). If we look at what prints for pi we see that it is the same as &i, the actual value in the variable pi is the address of i. The last part '*pi' applies the indirection operator to pi and displays the value of what pi points at.

With me so far? Lets modify that last program a bit to see what happens.
#include <stdio.h>

int
main(int argc, char **argv)
{
    int i = 100;
    int n = 200;
    int *pi = &i;
    /* %x is a printf format arg to print values in hexadecimal */
    printf("initial state)\n");
    printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
      i, n, &i, &n, pi, *pi);

    pi = &n;
    printf("(pi = &n)\n);
    printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
      i, n, &i, &n, pi, *pi);
    *pi = 42;
    printf("(*pi = 42)\n");
    printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
      i, n, &i, &n, pi, *pi);
    printf("(pi = &i)\n"); 
    pi = &i;
    printf("i = %d, n = %d, &i = %x, &n = %x, pi = %x, *pi = %d\n",
      i, n, &i, &n, pi, *pi);
    return 0;
}

This is sort of a "poke it with a stick and see what happens" approach. We start with the same program from the last example, then make a change to some of the values, print out what change we made and then print out the results of the change.

On my system it prints the following:
(intitial state)
i = 100, n = 200, &i = bffffcc0, &n = bffffcc4, pi = bffffcc0, *pi = 100
(pi = &n)
i = 100, n = 200, &i = bffffcc0, &n = bffffcc4, pi = bffffcc4, *pi = 200
(*pi = 42)
i = 100, n = 42, &i = bffffcc0, &n = bffffcc4, pi = bffffcc4, *pi = 42
(pi = &i)
i = 100, n = 42, &i = bffffcc0, &n = bffffcc4, pi = bffffcc0, *pi = 100

One thing is to notice that the address of n and i (&n, &i) never changed. The value of pi was always the address of whichever int variable we had set pi to point to. The only thing thing that might come as a surprise is after the (*pi = 42) line, it immediately changed the value of the variable n that pi was pointed at at that moment, so not only can we look at the thing pointed to, we can change it as well.

So the most important thing to get here is that a pointer contains an address of another variable. The two different meanings of '*' as part of the pointer variable type itself and as the indirection operator may be a bit confusing for a while, but as you work with them it will be clear what the '*' means at any given moment.

The thing that may remain a bit confusing is what pointers are really good for. Why do you care about the address of a variable when you have the variable itself? I'll put together a tutorial on arrays and memory allocation that I hope will make that a bit more clear.
Ashcroft is offline   Reply With Quote
Old Sep 19th, 2004, 2:39 PM   #2
Overmind
Professional Programmer
 
Overmind's Avatar
 
Join Date: Jun 2004
Location: South Africa, Johannesburg
Posts: 301
Rep Power: 5 Overmind is on a distinguished road
Thanks, I think this will help me, as c++ for dummies, maybe I can understand now.
__________________
[SIGPIC][/SIGPIC]
Overmind is offline   Reply With Quote
Old Sep 19th, 2004, 3:52 PM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Here's a tip: ditch that book. It's crap. I've never come across a good C++ book (but then, I haven't looked very hard - I learnt C and went from there). Perhaps someone else could recommend something?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Sep 19th, 2004, 4:00 PM   #4
Ravilj
Programmer
 
Ravilj's Avatar
 
Join Date: Sep 2004
Location: JHB , South Africa
Posts: 79
Rep Power: 5 Ravilj is on a distinguished road
A great book for console programming that teaches all the basics:
Deitel and Deitel: How to program C++
ISBN: 0-13-111881-1
Publisher: Prentice Hall

They also have C# Jave et al languages... very nicely layed out and good examples and explanations.
__________________
Ravilj's OpenGL Terrain aka WinTerrain Last Updated: 17/01/2005!
Ravilj is offline   Reply With Quote
Old Sep 19th, 2004, 10:10 PM   #5
Overmind
Professional Programmer
 
Overmind's Avatar
 
Join Date: Jun 2004
Location: South Africa, Johannesburg
Posts: 301
Rep Power: 5 Overmind is on a distinguished road
Quote:
Here's a tip: ditch that book. It's crap. ve never come across a good C++ book (but then, I haven't looked very hard - I learnt C and went from there). Perhaps someone else could recommend something?
All I can afford is free e-books.
__________________
[SIGPIC][/SIGPIC]
Overmind is offline   Reply With Quote
Old Oct 1st, 2004, 6:13 AM   #6
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 556
Rep Power: 5 Benoit is on a distinguished road
I learned a lot of C from C by example by Greg Perry...You should try C++ by example
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Oct 1st, 2004, 7:55 AM   #7
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 556
Rep Power: 5 Benoit is on a distinguished road
Nice tutorial...Let me get this straight

If I had int i; and int *pi, and *pi=&i, pi would be the value of i because the address of i was assigned to it?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Oct 1st, 2004, 2:54 PM   #8
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
pi would be the memory address of i.
__________________
&quot;Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Oct 1st, 2004, 3:04 PM   #9
Benoit
Expert Programmer
 
Benoit's Avatar
 
Join Date: Sep 2004
Location: Ontario, Canada
Posts: 556
Rep Power: 5 Benoit is on a distinguished road
And *pi would be the value of i?
__________________
Johnny was a chemist's son but Johnny is no more, for what Johnny thought was H2O was H2SO4
Benoit is offline   Reply With Quote
Old Oct 1st, 2004, 4:17 PM   #10
Ashcroft
Programmer
 
Join Date: Sep 2004
Posts: 38
Rep Power: 0 Ashcroft is on a distinguished road
Quote:
Originally posted by Benoit@Oct 1 2004, 08:04 PM
And *pi would be the value of i?
Pretty much. *pi actually is i, so you can read the value of i through it - or change the value of i through it.
Ashcroft is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC