Pointers in C: A Beginner’s Guide

A pointer is a variable used to store a memory address. Pointers enable you to pass values by reference (modify values in memory) and to work with dynamic data structures such as queues and stacks.

In this article, developers will learn how they can define pointers and what operations you can perform with them.

Creating Pointers in C

You can create pointers to variables of any data type. To create a pointer, state the data type followed by an asterisk (*) and the pointer name, as in the following example:

int *countPtr;

You can also define a pointer by placing the asterisk in front of the data type. The first syntax is preferred, though:

int* countPtr;

Unlike when defining normal variables, the * operator does not distribute to all the pointer variables. Observe the following C code:

int *b, y;

In the above statement, only the variable b is a pointer of type int. Meanwhile, y is just a variable of type int.

If you want to declare multiple pointers in the same statement, then you need to add an asterisk before each pointer variable, as shown below:

int *bPtr, *yPtr ;

How to Initialize Pointers in C

Pointers must always be initialized when defined. You can initialize a pointer with NULL, 0 or an address. Initializing a pointer to NULL is the same as initializing it to 0 (zero).

A pointer initialized to any of these two values (NULL or 0) points to nothing.

The third way to initialize a pointer is to assign an address to it. The reference operator (&) is used to assign a memory address to a pointer. It’s a unary operator that takes in a variable as its operand. Here it is in use:

int y;
yPtr = &y;

It is important for you to note that a constant or an expression can’t be used as an operand to the & operator.

As mentioned earlier, pointers store memory addresses. If you want to get the memory address which your pointer is storing, then you would use the dereferencing operator (*).

This operator will return a hexadecimal value that indicates the memory location which your pointer is storing. See the example below:

# include  

int y = 7;
int z;

int yPtr = &y;
int zPtr = &z;

printf("The pointer yPtr is storing the address %p: " , &y); 
printf("\n The value stored by *yPtr is: ", *yPtr);

The code example above demonstrates how you can use the address operator and the dereferencing operator.

Other Pointer Types in C

Apart from the null pointer, there are also other pointer types that are worth mentioning. We discuss them below.

Void Pointer in C

The void pointer is a pointer that can store an address of any data type. It is also known as a generic pointer.

To define a void pointer in C, use the keyword void in your pointer declaration. Here is some example code:

int main() 
{
  void  *aPtr;
  void  *bPtr;
  
  int a = 6;
  char b = 'G';

  aPtr = &a; // aPtr stores a pointer to an integer 
  bPtr = &b; // bPtr stores a pointer to a character 
}

Wild Pointer in C

A wild pointer is a pointer that has not yet been initialized. This pointer points to an arbitrary location in memory. You should generally never use such a pointer. Doing so could crash a running program in memory. That being said, here is how you use wild pointers:

float  *Yptr; // this a wild pointer. Don't use it until you initialize it

Passing Values by Reference in C

There are two ways in which you can pass arguments to a function. You can either pass-by-value or pass-by-reference.

Pass-by-value means that the value of the argument is passed to the function while pass-by-reference means that a pointer to the argument is passed.

By default, all arguments are passed by value in the C language. Pass-by-value first makes a copy of the argument and then passes on this copy to the function. This introduces overheads in processing and also leads to high memory usage.

Pointers can be used to overcome this challenge since they work directly with variables in their addresses.

The code example below shows how you can get the square of an integer using pass-by-value and pass-by-reference:

#include 

void SquareByValue (int p){
p*p; }

void SquareByReference (int *qPtr){
*qPtr * *qPtr;  }

int main (){

int a = 4;
int b = 6;
}

C Pointers

Unlike Java and Python, C gives you the option to directly work with memory addresses for your program.

As you must be aware by now: “With great power comes great responsibility.” If you inappropriately allocate memory addresses, you could end up crashing programs running on your computer.

Pointers should be used with care. In particular, you should avoid wild pointers.

Read: Data Types, Variables, and Constants in C++.

More by Author

Must Read