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.

// int = the type of variable the pointer points to
// *P_Name1 indirection operator and pointer name
int   *P_Name1

// char = the type of variable the pointer points to
// *P_Name2 indirection operator and pointer name
char  *P_Name2

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.

// int = the type of variable being declared
// *P_Name pointer name which points to an integer
// Result standard integer variable being declared
int   *P_Name, Result;

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.

// & or reference operator assigns the address of Variable_Name to
// the pointer P_Name
P_Name = &Variable_Name

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.

// int = the type of variable being declared
// *P_Name pointer name which points to an integer
// Result standard integer variable being declared
int   *P_Name, Result;

// & or reference operator assigns the address of Result to P_Name
P_Name = &Result;

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.

int main(void)
{
	// Declare and initialize an int variable named Result
	int Result = 10;

	// Declare a pointer to an int variable named P_Name
	int *P_Name;

	// Initialize the pointer P_Name to point to Result
	P_Name = &Result;

	// Demonstrates ways to access the value of Result,
	// both directly and the indirectly using the pointer
	printf("Direct Access, Result = %i\n", Result);
	printf("InDirect Access, Result = %i", *P_Name);

	// Demonstrate 2 ways to display the address of Result
	printf("\nThe address of Result = %i\n", &Result);
	printf("The address of Result = %i", P_Name);

	return 0;
}

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.

int main(void)
{
    int a;
    float b;
    double c;
    char d;

    printf("Size of int: %i bytes\n",sizeof(a));
    printf("Size of float: %i bytes\n",sizeof(b));
    printf("Size of double: %i bytes\n",sizeof(c));
    printf("Size of char: %i byte\n",sizeof(d));

    return 0;
}

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