Click here to Skip to main content
12,813,879 members (31,631 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 11 Jul 2003

Some details about pointers

, 11 Jul 2003
Rate this:
Please Sign up or sign in to vote.
An article which has the purpose to help beginners on using pointers.


Any new programmer in C or in C++ tries usually to avoid using pointers in his program and perhaps he doesn't show a real interest to programs using them. This behavior is understandable because it isn't so easy for a new programmer to deal, alone, with pointers, and I can feel what a new programmer feels because every one is a beginner at first and none began as an expert. This tutorial will help you to deal with pointers and use them efficiently.

What pointers are?

A pointer designates a variable which contains the address of another. Let's begin with the concept of address. Memory is divided into a certain number of cells or boxes. Each box contains 8 binary information or bits. That constitutes a byte (= 8bits). What is more important is that each box has an identifier. This ID is called the address.

During the execution of a program, some boxes are reserved for each variable. The number of boxes reserved is related to the type of the variable you need. As we can accede to variables by name (which is the most common), we can also, using pointers, accede to them by address. Assembly programmers may see the similarity between the concept of indirect addressing and using pointers which represents the strength of C programming. Pointers have the mission of facilitating access to variables, arrays, functions, objects... So, you can permute two variables without moving them from there physical location into memory, and you just have to change there addresses so you can make your application faster. Perhaps you ask the question: I haven't remarked any difference between the two methods, but imagine you have to make thousands of permutations, then your application may need a powerful machine to do that. So, you need to manage some optimizations in your program in order to assure your program to run into a normal machine. This is normally done by using pointers. You can feel what pointers can offer to you when you use the operators new and delete like in the following example:

Cout <<" how many elements you want?";
Cin >> N;
Int* vect = new int[N];
delete[] vect;

Let's begin:

Before using a pointer, we should declare it like we do with almost all variables, and here the type of variable we need to point to is specified. Let's deal with this example:

char* p; or char *p;

Whether the first expression or the second is written the compiler doesn't care, but I really prefer the first because that reflects that we need to modify the variable type, and you can see NITRON's article: "A Prelude for pointers" for more details. Then:

  • char: indicates the type of the pointed variable.
  • *: indicates that we mean a pointer.
  • p: the name of the variable.

Use of pointers:

Try this code:

int i , *p;
i = 7;
p= &i; 
printf ( "%d", i); 
printf ( " %d", *p);

You will remark that the output of this program will be 7 7, and this is predictable since p represents the address of i. The preceding example shows that we need not accede to variables directly, but we can simply use addresses and that makes our program more professional and much quicker. Let's precise an important but invisible thing: when you increment or decrement a pointer, the unit used is the size of the pointed variable. This is very important when using arrays since incrementing makes our pointer pointing on the next element.

Using pointers with arrays:

Pointers are a powerful tool when using arrays. This criteria is clear in the following program:

int a[10] , *p, r ;
a[0]=2; a[1] =3; a[2] = 5; a[3] =6;
p = & a[0];
r=*p +1;
r= *(p+1);

let's comment this program:

p = & a[0]; => that expression means that we want our pointer to point on the first element of the array. Note that we can also write p=a;, and this is enough because the name of the array represents by itself a pointer on the first element of the array, but never increment or decrement it.

r = *p; => copy the value of the element pointed by p. I.e., a[0] which is equal to 2.

p++; => increment p. p is equal, now, to the address of a[1].

r=*p; => r = the content of a[1] which is 3.

r=*p +1; => copy into r the value of a[1] added to 1 and then r = 4.

r= *(p+1); => parentheses have the priority here that we begin with the expression p + 1, that means that we point on address of a[1] incremented (the address) by 1, i.e., the address of a[2]. Then *(p+1) returns the value of a[2].

Note that p still points to a[1].

But don't confuse pointers and arrays!

When learning the C language, we can be under the impression that both pointers and arrays represent the same thing. Here, you should be careful because that isn't the truth.

Let's give an example:

When we write: int A[5], we declare an array and our compiler reserves a space of 5 consecutive integers. However, when we write: int* p, we just reserve a place for one element which contains an address.

The programmer in C generally confuses pointers and arrays because the identifier of an array (A in our example) is converted to a pointer of its type (int in our example).

Another cause of making the mistake is the equivalence between A[i] and *(A+i). But remember that you cannot write A++; but you can write p++;. Also, we can declare an array of integers writing: int A[] = { 0,2,5,8,7}, but you cannot declare a pointer on an array of integers writing int* p = {0,2,5,8,7}.


In the text above, I have tried to give a summary for using pointers and explained the reasons that make us prefer using this concept to classic methods. A big truth that should never be forgotten is that nothing becomes easier without practicing and reserving much time to improve our capacities. I wish this article to be useful and my English to be coherent and understood, because I have not the habit to write in English.

Points of Interest:

I am a student in electrical engineering in Tunisia. I have developed more than an application in the field of microelectronics. Now the thing which is interesting me more is designing a graphical interface for commanding an industrial batteries charger I have made.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Ben Khalifa Wassim
Web Developer
France France
I am a student in electrical ingineering in Tunisia. I have had the superior technicien diploma after mading an intelligent batterie charger.

You may also be interested in...

Comments and Discussions

GeneralError in the 2 last images Pin
inspeckpr15-Jul-03 2:48
memberinspeckpr15-Jul-03 2:48 
GeneralRe: Error in the 2 last images Pin
Ben Khalifa Wassim15-Jul-03 11:11
memberBen Khalifa Wassim15-Jul-03 11:11 
GeneralRe: Error in the 2 last images Pin
inspeckpr15-Jul-03 11:41
memberinspeckpr15-Jul-03 11:41 
GeneralRe: Error in the 2 last images Pin
Ben Khalifa Wassim15-Jul-03 16:09
memberBen Khalifa Wassim15-Jul-03 16:09 
GeneralRe: Error in the 2 last images Pin
Ben Khalifa Wassim15-Jul-03 16:14
memberBen Khalifa Wassim15-Jul-03 16:14 
GeneralMemory Leak Pin
whizer12-Jul-03 23:38
memberwhizer12-Jul-03 23:38 
GeneralRe: Memory Leak Pin
Ben Khalifa Wassim13-Jul-03 1:50
memberBen Khalifa Wassim13-Jul-03 1:50 
GeneralRe: Memory Leak Pin
Roshmon13-Jul-03 22:59
memberRoshmon13-Jul-03 22:59 
GeneralRe: Memory Leak Pin
Ben Khalifa Wassim14-Jul-03 0:17
memberBen Khalifa Wassim14-Jul-03 0:17 
Generaldeja vu Pin
dog_spawn12-Jul-03 14:36
memberdog_spawn12-Jul-03 14:36 
GeneralRe: deja vu Pin
Ben Khalifa Wassim13-Jul-03 1:48
memberBen Khalifa Wassim13-Jul-03 1:48 
GeneralRe: deja vu Pin
dog_spawn13-Jul-03 3:15
memberdog_spawn13-Jul-03 3:15 
GeneralRe: deja vu Pin
Ben Khalifa Wassim13-Jul-03 10:20
memberBen Khalifa Wassim13-Jul-03 10:20 
Generalyes, diagrams Pin
dog_spawn16-Jul-03 15:49
memberdog_spawn16-Jul-03 15:49 
GeneralRe: yes, diagrams Pin
Ben Khalifa Wassim17-Jul-03 8:55
memberBen Khalifa Wassim17-Jul-03 8:55 
Generalemail Pin
dog_spawn18-Jul-03 6:10
memberdog_spawn18-Jul-03 6:10 
GeneralPossible improvement Pin
Stephane Rodriguez.12-Jul-03 10:04
memberStephane Rodriguez.12-Jul-03 10:04 
GeneralRe: Possible improvement Pin
Ben Khalifa Wassim12-Jul-03 10:38
memberBen Khalifa Wassim12-Jul-03 10:38 
GeneralRe: Possible improvement Pin
Stephane Rodriguez.12-Jul-03 10:48
memberStephane Rodriguez.12-Jul-03 10:48 
GeneralRe: Possible improvement Pin
Ben Khalifa Wassim15-Jul-03 11:15
memberBen Khalifa Wassim15-Jul-03 11:15 
GeneralRe: Possible improvement Pin
Oha Ooh14-Jul-03 10:51
memberOha Ooh14-Jul-03 10:51 
GeneralRe: Possible improvement Pin
Ben Khalifa Wassim15-Jul-03 11:18
memberBen Khalifa Wassim15-Jul-03 11:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 12 Jul 2003
Article Copyright 2003 by Ben Khalifa Wassim
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid