Javatpoint Logo
Javatpoint Logo

How to use Pointers in C language?

Pointers are like normal variables, but in the place of storing variable value, pointers store the address of another variable or another pointer. A pointer can hold the addresses of variable of different data types-integer, char, or even an array. When a pointer is holding an address of a variable, we say that the pointer is pointing to that variable. This article explains the basics of pointers for beginner-coders.

Syntax:

Points to understand:

  • * -> Indirection Operator -> denotes "value at".
  • & -> Address Operator -> denotes "the address of"
  • "*" gives the value stored in the variable, whose address the pointer is pointing at. Using "&" gives the address of the variable or pointer.
  • The address of a variable or an entity is dependent on the OS or the machine. We can't predict the address of an entity unless it's in an array and the base address and the data type are known.
How to use Pointers in C language

Example:

Here, p is a pointer of data type integer.

  • A pointer of a specific data type can only store the address of that data typed entity. If we try to store the address of another data type variable, it raises a warning.

Example:

Warning:

warning: assignment to 'int *' from incompatible pointer type 'float *

Important points:

  1. A pointer always has whole numbers stored in it because it stores the address, and the address can't be negative or a fraction.
  2. A pointer is always initialized to null, and the value of a null pointer is 0.
    int *p = null;
    Then, we give it the address we want for no confusion as always a declared variable needs to be initialized to 0 and so pointer to null.
  3. Here, if a pointer is initialized to NULL, it is not pointing to any entity.

Simple example program:

Output:

Value of  b =50
Address of  b =6487572
Address of  ptr =6487576
Value of  ptr =50
The pointer stores =6487572

As you can see, the value of b and the value of *ptr are the same, as ptr stores the value of b. The address of the variable b is the same as the value in the pointer. The address of the pointer is independent.

How to use Pointers in C language

Referencing and Dereferencing operators:

Referencing: It is taking the address of an entity using "&."

Dereferencing takes the value at operator "*" to access the value at the pointer.

  • Hence, "&" and "*" are also called reference and dereference operators.
How to use Pointers in C language

Arithmetic on Pointers

Suppose, an integer pointer is pointing to a variable at address 500:

Expression Evaluation
ptr = ptr + 1 ptr = 500 + 1*4 = 504
ptr++ or ++ptr 504 + 1*4 = 508
ptr = ptr + 4 508 + 4*4 = 524
ptr = ptr - 2 524 - 2*4 = 516
ptr -- or --ptr 516 - 1*4 = 512

Remember that 4 is the size of the integer data type.

Another example:

Code:

Output:

How to use Pointers in C language

Pointer to pointer:

As mentioned in the definition, a pointer can store the address of another pointer, and then it is called a double-pointer. It is also known as pointer-to-pointer.

How to use Pointers in C language

Syntax:

Example program:

Output:

a = 65
address of a = 6487572
ptr1 = 6487572
address ptr1 = 6487560
*ptr1 = 65
ptr2 = 6487560
*ptr2 = 6487572
**ptr2 = 65

Pointer to Arrays

  • For an array, the base address of the array is the address where the very first element at the 0th index is stored.
  • When we declare an array, the compiler allocates the memory to contain all the array elements and gives the base address.
How to use Pointers in C language

Here, ptr is the pointer pointing to the first element of the array (arr[0])

Points to understand:

For an array:

int arr[4], a pointer say ptr is pointing at arr[0], then:

  • &arr[0] is equivalent to ptr.
  • arr[0] is equivalent to *ptr.
  • &arr[1] is equivalent to ptr+1 and arr[1] is equivalent to *(ptr+1).
  • &arr[2] is equivalent to ptr+2 and arr[2] is equivalent to *(ptr+2).
How to use Pointers in C language

Example program:

Output:

Enter 6 numbers: 2 3 4 5 6
0
Sum = 20

Understanding:

Here, x[6] is the array to which by default x is the pointer pointing to its base address. So, instead of x[i], we can say x+i as mentioned above.

Using our own pointer to array:

Output:

*ptr = 3
*(ptr+1) = 4
*(ptr-1) = 2

Pointer as a Function argument

Pre-requisite: call by value (functions)

To pass arguments in the function call to the function declaration, we can either use call by value in which we pass the values, and here is the second and more efficient way- Call by Reference, where we pass the addresses to the pointers.

Example program:

Output:

a = 10
b = 20

*m and *n = 10 and 20
Now, *m = 20 and *n = 10

After Swapping:

a = 20
b = 10

Understanding:

Here, we defined a function "swap" to swap the values of two variables, "a" and "b". We gave the values 10 and 20 to "a" and "b," respectively. We defined the function with two pointers, *m and *n. Now, we pass the addresses of these two arguments to the pointer parameters. So, in the function call, we pass "&a" and "&b" to *m and *n in the declaration. Now, we swap the values using *(value at) pointers.

  • If we use "call by value", any change made to the reference variable (parameters) does not affect the original variable (arguments).
  • But, when we use this "call by reference", any change on the reference variable affects the original variable.

This can be observed in the function declaration. The values of *m and *n are also modified along "a" and "b".

Pointer to Structure

Pre-requisite: Structures in C

When we want to access a member in a structure using its variable, we use the "." operator.

If we declare a pointer of structure type, then we use the "->" operator to access the members of the structure.

Example:

Here,

employeePtr -> age is equivalent to (*employeePtr).age

employeePtr -> weight is equivalent to (*employeePtr).weight

Example program:

Output:

NAME: Rishab
NUMBER: 100
RANK: 1

Self Referential Structure

These are the structures that consist of one or more pointers that point to the same structure as their members.

How to use Pointers in C language

To make it clear, Structures that point to the same type of structures are "Self-referential structures".

Example program:

Output:

30
40

Features of Pointers

  1. Saves memory space
  2. Execution time is faster because of the directly accessed memory locations of the values.
  3. Useful to represent multi-dimensional arrays and other data structures
  4. Used for file handling
  5. Efficient access to the memory-dynamic memory allocations.

Interview questions on Pointers

1. What do you think will be the output of this program?

Solution:

On execution, this program gives a compile-time error.

error: invalid use of void expression

This is because "vptr" is a void pointer. Void pointers cannot be dereferenced. If we want to, then we need to typecast it and then do the dereferencing.

Void pointer: The pointer without any data type associated with it.

2. What will be the output of this program?

Answer:

Here, the variable k is a double pointer pointing to "i" indirectly. But, k is incremented. After incrementing, this final value is some garbage value that is not the address of any other variable. Hence, It could result in a segmentation fault or a garbage value being printed.

3. What will the output of this program be?

Answer:

Here, the pointer j is pointing to i. But, in the next step, j is incremented. Now, j neither has any address nor any value. Rather, it has a garbage value stored in it as we did not initialize anything to the address next to the address of the variable "i".

4. What are the differences between call by value and call by reference?

S. No Call by Value Call by reference
1. The copy of the argument in the function call is made into the parameters in the declaration, and the values are passed. Here, the address of the arguments is passed to the pointer-parameters in the function declaration, and hence the values are passed.
2. Changes in the function are restricted to the function only. The values of the actual parameters do not change. With the modifications inside the function, the actual parameters, as well as reference parameters all, are modified/
3. Actual and formal parameters belong to different memory locations Actual and formal parameters are created at the same memory space

5. What are the most significant uses of pointers in any programming language?

Answer: Pointers are used in many scenarios. But, out of those many uses, some of the significant uses are:

  1. For implementing dynamic memory allocation.
  2. To implement different data structures.
  3. To perform system-level programming.
  4. To implement call-by-reference
  5. For accessing array members without complex syntaxes and confusion.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA