Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Some details about pointers

Rate me:
Please Sign up or sign in to vote.
1.90/5 (22 votes)
11 Jul 20035 min read 80.6K   17   22
An article which has the purpose to help beginners on using pointers.

Introduction

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.

Image 1

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:

main()
{
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:

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

let's comment this program:

Image 2

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.

Image 3

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

Image 4

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

Image 5

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

Image 6

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].

Image 7

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}.

Conclusion:

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.

License

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


Written By
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.

Comments and Discussions

 
GeneralError in the 2 last images Pin
inspeckpr15-Jul-03 1:48
inspeckpr15-Jul-03 1:48 
GeneralRe: Error in the 2 last images Pin
Ben Khalifa Wassim15-Jul-03 10:11
Ben Khalifa Wassim15-Jul-03 10:11 
GeneralRe: Error in the 2 last images Pin
inspeckpr15-Jul-03 10:41
inspeckpr15-Jul-03 10:41 
GeneralRe: Error in the 2 last images Pin
Ben Khalifa Wassim15-Jul-03 15:09
Ben Khalifa Wassim15-Jul-03 15:09 
GeneralRe: Error in the 2 last images Pin
Ben Khalifa Wassim15-Jul-03 15:14
Ben Khalifa Wassim15-Jul-03 15:14 
GeneralMemory Leak Pin
whizer12-Jul-03 22:38
whizer12-Jul-03 22:38 
GeneralRe: Memory Leak Pin
Ben Khalifa Wassim13-Jul-03 0:50
Ben Khalifa Wassim13-Jul-03 0:50 
GeneralRe: Memory Leak Pin
Roshmon13-Jul-03 21:59
Roshmon13-Jul-03 21:59 
GeneralRe: Memory Leak Pin
Ben Khalifa Wassim13-Jul-03 23:17
Ben Khalifa Wassim13-Jul-03 23:17 
Generaldeja vu Pin
dog_spawn12-Jul-03 13:36
dog_spawn12-Jul-03 13:36 
GeneralRe: deja vu Pin
Ben Khalifa Wassim13-Jul-03 0:48
Ben Khalifa Wassim13-Jul-03 0:48 
GeneralRe: deja vu Pin
dog_spawn13-Jul-03 2:15
dog_spawn13-Jul-03 2:15 
GeneralRe: deja vu Pin
Ben Khalifa Wassim13-Jul-03 9:20
Ben Khalifa Wassim13-Jul-03 9:20 
Please I want remarks that makes me improve the article. I mean technical remarks gneral criticals do not make that.
you should perhaps propose another subject to treat for example.
I am here to receive your mails privetly if you want at
Wassim.mail@compaqnet.fr
thank you very much for contribution sir.

Ben khalifa Wassim
Generalyes, diagrams Pin
dog_spawn16-Jul-03 14:49
dog_spawn16-Jul-03 14:49 
GeneralRe: yes, diagrams Pin
Ben Khalifa Wassim17-Jul-03 7:55
Ben Khalifa Wassim17-Jul-03 7:55 
Generalemail Pin
dog_spawn18-Jul-03 5:10
dog_spawn18-Jul-03 5:10 
GeneralPossible improvement Pin
Stephane Rodriguez.12-Jul-03 9:04
Stephane Rodriguez.12-Jul-03 9:04 
GeneralRe: Possible improvement Pin
Ben Khalifa Wassim12-Jul-03 9:38
Ben Khalifa Wassim12-Jul-03 9:38 
GeneralRe: Possible improvement Pin
Stephane Rodriguez.12-Jul-03 9:48
Stephane Rodriguez.12-Jul-03 9:48 
GeneralRe: Possible improvement Pin
Ben Khalifa Wassim15-Jul-03 10:15
Ben Khalifa Wassim15-Jul-03 10:15 
GeneralRe: Possible improvement Pin
Oha Ooh14-Jul-03 9:51
Oha Ooh14-Jul-03 9:51 
GeneralRe: Possible improvement Pin
Ben Khalifa Wassim15-Jul-03 10:18
Ben Khalifa Wassim15-Jul-03 10: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.