Pointers in C

What is a Pointer?

Pointer is kind of variable that can store the address of a memory location of another variable.


We use variables to store data. We know that data is stored actually in some location in memory which does have some address. As shown in the illustration above the variable 'a' is stored in memory location whose address is 0002. Similarly b is stored in 1008 memory location. This is just a illustration. Obviously in your system 'a' and 'b' will have different addresses. Don't think about content column and the pointer associated with it now.

The Address Operator (&)

You have used ampersand (&) operator with scanf() function. The ampersand (&) operator gives the address of a variable. Remember using scanf("%d",&a)? So what is the meaning of that?

&a gives address of a. In our case it is 0002.
&b will yield 1008. So it helps scanf() function to save the input at that memory location.
Ampersand (&) is unary operator. This means it can be used with only one operand.

&(a+b),&57, &('x') are all invalid while &variable_name like &n,&a,&b,&acb12 are all valid.

Declare A Pointer

According to the definition, pointer is a variable like any other, so before it can be used we must declare it.

Syntax:
data_type * pointer_name;
data_type can be any valid C data type. Pointer name is any valid variable name. The asterisk (*) is the indirection operator and it indicates that pointer_name is a pointer variable and that stores the address of a variable of the specified data type.

Example:
int number; // Integer type variable
int * p1;  // Integer type pointer variable
Here the integer type pointer variable p1 can store the address of any integer type variable.

Initializing Pointers

By using address-of operator (&) we can get address of any variable and then we just have to assign it to a pointer variable of the same data type. Assigning an address to a pointer is called pointer initialization.

Syntax:
pointerName=&variableName
Example:
int score; // Integer type variable
int * p,*p1;  // Integer type pointer variable

p=&score; Assigning the address of score to to p

int arr[10];

p1=arr // Assigning the base address of array to p

De-referencing

De-referencing is the operation performed to access or manipulate data contained in the memory location pointed by a pointer. The indirection (*) operator (also known as value-at operator) is used with a pointer variable, it refers to the variable being pointed to. This is de-referencing of pointers. See syntax and example below to better understand.

Syntax:
*pointerName
Example:
int age=19,*p; // Declaring a variable and pointer of type int

p=&age; Assigning the address of age to to p

printf(" %d ",*p); // Displays 19

Summary of de-referencing

  1. The variable name refers to actual data. Pointer name refers to the address.
  2. If a pointer ptr is pointing to a variable csa then,
    • Both csa and *ptr refer to the contents of csa
    • Both &csa and ptr refer to the address of csa

Void Pointer

The void type of pointer is special kind of pointer. Void means no data type specifically. So void pointers are the pointers that point to value that has no type. This allows void pointers to point to any data type (integer,float,char,double). But these pointers cannot be directly de-referenced. To use them,these pointers have to typecast. We will see typecasting in upcoming lessons.

Syntax:
void * pointer_name;

Advantages of pointers in C

Pointers provide direct access to memory Pointers provide a way to return more than one value to the functions Reduces the storage space and complexity of the program Reduces the execution time of the program Provides an alternate way to access array elements Pointers can be used to pass information back and forth between the calling function and called function. Pointers allows us to perform dynamic memory allocation and deallocation. Pointers helps us to build complex data structures like linked list, stack, queues, trees, graphs etc. Pointers allows us to resize the dynamically allocated memory block. Addresses of objects can be extracted using pointers.

Drawbacks of pointers in C

Uninitialized pointers might cause segmentation fault. Dynamically allocated block needs to be freed explicitly. Otherwise, it would lead to memory leak. Pointers are slower than normal variables. If pointers are updated with incorrect values, it might lead to memory corruption. Basically, pointer bugs are difficult to debug. Its programmers responsibility to use pointers effectively and correctly.

C Programs For Practice:

  1. C program to print address of memory location using a pointer
  2. C program to calculate sum of array elements using pointer
  3. C program to swap two numbers using pointers