C programming pointers tutorial

C Programming Pointers Pt/1

This first tutorial will be a basic introduction to C programming Pointers.  They are an important part of the C language, providing a powerful and flexible method of manipulating data.  When pointers are used with functions, it allows the values of variables to be changed regardless of where they originated.

Declaring A Pointer

The code snippet below shows how to declare a basic pointer, there are 2 examples showing an int and a char variable.

So the first part of the code int and char in this case, is the type of variable the pointer points to.  The next part which starts with an asterisk (*) is known as the Indirection operator.  The indirection operator tells the compiler P_Name1 is a pointer to an integer and P_Name2 is a pointer to a char.  So the indirection operator tells the compiler which type of variable the pointer name points to, in this case either an integer or a char.  It is important to note that these are pointers and not variables, the compiler knows this due to the indirection operator, and the context in which it has been used.

Pointers can also be declared along with other non pointer variables, an example can be seen below.

Initializing Pointers

Initializing the pointer basically ensures the pointer actually points to something.  This is carried out by the pointer storing the memory address, of the variable it points to.  The ampersand (&) or Reference operator is used to do this, as it returns the address of the variable   An example below shows how the pointer is initialized.

The ampersand is placed before the variable we want the pointer to point to.  When placed before the name of a variable, the reference operator returns the address of the variable. The next example demonstrates a similar initialization, but the pointer and integer declarations have been added as well.

The image below shows a screen capture from Eclipse.  The variable Result has been initialized with a value of 10 (this has been done to avoid some random value being displayed), The value of P_Name is the memory address location of Result, and *P_Name has the same value as Result.

C Programming Pointer Tutorial Eclipse

Basic Example Of Pointer Usage

The next example builds on the previous code, by using similar named variables.  It also demonstrates how the indirection, and reference operators can be used in a basic form.

The result of this basic program is displayed below with a screen capture from Eclipse.

C Programming Pointer Tutorial EclipseSo what we can see here, well the variable Result was declared and initialized with a value of 10,  in line 4.  The first line from the image which reads “Direct Access, Result = 10” shows the value of Result printed, by directly accessing the variable value.  The second line which reads “InDirect Access, Result = 10” demonstrates indirect access to the value of Result, by using the pointer P_Name, which was initialized to point to Result in line 10.Line 15 of the program uses the indirection operator, to print the value stored in the address location pointed to by P_Name.

The third line which reads “The address of Result = 2686696” is produced by line 18 of the code.  This uses the reference operator to print the address of Result, this is the actual memory address of variable Result in the computer memory.  The forth line which reads the same as the third line, uses the pointer P_Name which points to the memory address of Result in line 19.

To summarize whats happening here:

*P_Name and Result both refer to the contents of Result

P_Name and &Result both refer to the memory address of Result

Computer Memory

At this stage it’s important to talk a little about computer memory as well as variable types.

When a variable is declared inside a C program, the compiler sets aside a unique memory location for that variable.  In the last example the pointer P_Name points to the address of the variable Result, and the program demonstrated how the memory location address could be displayed.  By having unique names for variables it ensures the correct memory location is accessed, the memory addresses are generally hidden from you and not usually used.

Different variable types consume different amounts of memory i.e. int, char etc, also the architecture of the system determines variable sizes i.e. 8 bit, 16 bit etc.  The simple program below can be run on your system.

The output from this program can be seen in the image below, this shows the differing amounts of memory each variable type consume.

C Programming Pointer Tutorial EclipseWith this knowledge of how different data types consume varying amounts of memory bytes,  how does the pointer handle the address of multi-byte variables?  Quite simply the pointer points to the first (lowest) address byte of the variable, and the variable size is known by the compiler.

Examples Code

The link below contains the zip file with the complete C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

Pointer examples code

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

Leave a Reply