Introduction to Pointers

Pointers are variables that are used to store the address of a variable, function, structure, array, and strings. It is a very important and necessary concept in C programming and we found beginners struggling to grasp this concept. After reading this tutorial, you will feel confident about pointers.

Memory Layout

Before understanding pointer, you need to understand, the memory layout of a computer. As you know that whenever, we create a variable of any data type such as character, integer, float, etc, they are stored in computer memory. Computer memory is usually byte-addressable. That means data stores in byte format in each location of memory. Furthermore, each byte of memory has a unique address.

How data stores in Memory?

We are considering RAM only because a program under execution is usually available in RAM. Modern computers come with 2GB, 5GB and more. For example, the following figure shows the RAM memory segment starting from address 200 – 300. Let’s say we initialize two variables one is character type and the other is an integer type. Character type variable takes 1 byte of memory. Hence, one byte is allocated for it in memory. Similarly, integer type variable, in typical standard computers, takes 4 bytes of memory. Therefore, 4 bytes are allocated for integer variable ‘x starting from address 200 to 203. The data stores in memory in binary and format of data storage depend on little-endian or big-endian whatever type of machine we are using.

C program memory overview

One more thing to note here is that the address of integer variable x is 200 and the address of character variable c is 205. If we perform any operation on these variables, the program reads the data from the same memory locations and saves back at the same location.

Note: When we declare a variable, memory space is allocated for it and the amount of space depends on the type of a variable.

Similarly, we can initialize other types of data types and they take memory space according to their type.

int x = 10; // takes 4 bytes of memory
Char c; // takes 1 byte of memory
Float y; // takes 4 bytes of memory

We if want to print the value of a variable, we use it name like this

Printf(%d/n", x); // Prints value of x

We can also get the address of a variable using & sign with a variable name like this:

printf("%d/n", &x) // prints address of variable x

What are the Pointers in C/C++?

In the previous section, we learned that when we define a variable, it stores in computer memory at some memory location and this memory location is the address of that variable. Now, what if we want to store the address of a variable? We can do it with the help of pointers. Let’s start with the definition of a pointer.

A pointer is a variable that is used to store the address of another variable. Pointers are also used to store the address of functions, structures, function callbacks.

Note: Unlike typical data types, a pointer variable is of fixed size. For 32 bit, it will be 4-bytes long.

For example, we have initialized an integer type variable x with value 10 and it stores at an address of 206 in memory. We declare another variable of type pointer to integer ptr and holds the address of variable x. A pointer variable Ptr also takes memory space and stores at memory location 202 as shown in the figure below:

Pointers introduction in c and c++

How to Declare Pointers in C and C++?

To declare a pointer variable, first, we specify the type of pointer and followed by a * asterisk sign. After that give the name to the pointer. For example, this line declares a pointer to an integer variable and pointer name is Ptr.

int *Ptr; // Ptr is a pointer to a integer variable
Char *Ptr1; // Ptr1 is a pointer to a character variable
Float *Ptr2; // Ptr2 is a pointer to a float variable

Lets say we want to write a pointer variable that points to an integer. For that we should define a integer variable ‘a’

int a;
int *Ptr; // Ptr is a pointer to a integer variable
//This line stores the address ofvariable 'a' to pointer variable //Ptr
Ptr = &a; 

The unary operator & is used to store the address of a variable to a pointer. For example, the address of a is 204. That means Ptr will hold a value of 204.

Pointers variable declaration in c and c++

A dereferencing operator that is * is used to get the value of a variable through a pointer variable. In other words, by using *Ptr we can get the value of x indirectly and also can be used to modify x value.

Printf("%d\n", Ptr); // prints the address of variable to which it point
Printf("%d\n", *Ptr); // prints the value of variable to which it points

Pointer Example Code

Now lets try to run this code and see the output.

#include<stdio.h>
#include<stdlib.h>
void main(void) {
	int x = 10; 
	int* y = &x;
	printf("Value of x = %d\n", x);
	printf("Address of x = %d\n", &x);
	printf("Value of x with *y\n = %d", *y);
	printf("Address of x  with pointer y= %d\n", y);
}

Output

As you can see from the output of above program, the address of variable x and value of pointer variable is same.

Value of x = 10
Address of x = 11532748
Value of x with *y = 10
Address of x  with pointer y= 11532748

Remember pointer displays virtual addresses, not physical addresses.

Now lets take few more example to learn pointer errors that you can face during compilation of a program.

Pointer Not Initialize Error Example

This example code generates an error. Unlike other data types, we can not simply declare a pointer variable without initializing it. If you try to do so, compiler will give error.

#include<stdio.h>
#include<stdlib.h>
void main(void) {
	int x;
	int* y;
	printf("Value of x = %d\n", y);
	printf("Address of x = %d\n", *y);	
}

Note: We must intitialize pointer with atleast NULL or 0 to prevent in to point to unknown memory.

Code Output

That means we can not use a poiner without initializing it.

Uninitialized local variable 'y' used

To avoid this error, we can assign a NULL value to an uninitialized pointer variable like this:

int* y = NULL;

Modify value using Pointer Example

We can also modify a variable value through a pointer dereferencing. Let’s take an example, in this code, first, we initialize the variable ‘x’ with the value 4. Pointer ‘y’ contains the address of variable x. After that, the print function prints the value of x indirectly through pointer y. To modify the value of variable x through a pointer, we use an assignment operation and store the value 10 to the memory location x.

#include<stdio.h>
#include<stdlib.h>
void main(void) {
	int x = 4;
	int* y = &x;
	printf("Value of x = %d\n", *y);
	*y = 10; // updata the value of x to 10
	printf("Modified Value of x = %d\n", *y);
}

Code Output

Value of x = 4
Modified Value of x = 10

Pointer Pointing to Uninitialized Variable

What will happen if a pointer variable points to a uninitialized variable? In this case, there will no error and pointer will successfully points to the address of a variable. But if we try to print a value of uninitialized variable directly or through a pointer dereferencing operator, it will either show a garbage value or zero depending on the scope of a variable.

If a variable is a local variable, its value will be a garbage value. But if a variable is either global or a static variable, the variable value will be zero. But in either case, a compiler will not give any error. Only the value of the uninitialized variable will be changed according to the the scope of the variable.

Now let’s see a demo code. In this example, we declare two integer variables, one is a local variable of a main() function and the second is a global variable. Also, we declare two pointers to integer variables. Now lets print values both variable through pointers and observe the output.

Code

#include<stdio.h>
#include<stdlib.h>
int a; // global variable
void main(void) {
	int b; //local variable
	int* Ptr1 = &a;
	int* Ptr2 = &b;
	printf("Value of global variable a = %d\n", *Ptr1);
	printf("Value of local variable b = %d\n", *Ptr2);

}

Code Output

Value of global variable a = 0
Value of local variable b = -858993460

Leave a Reply