0% found this document useful (0 votes)
2 views

Pointers

The document explains pointers in C programming, including their declaration, initialization, and dereferencing, as well as double pointers and pointers to arrays. It also covers the concepts of call by value and call by reference, highlighting how parameters are passed to functions. The document provides code examples to illustrate these concepts.

Uploaded by

Aniket Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Pointers

The document explains pointers in C programming, including their declaration, initialization, and dereferencing, as well as double pointers and pointers to arrays. It also covers the concepts of call by value and call by reference, highlighting how parameters are passed to functions. The document provides code examples to illustrate these concepts.

Uploaded by

Aniket Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

• A pointer is a variable that stores the memory address of another variable.

• Instead of holding a direct value, it holds the address where the value is stored in
memory. There are 2 important operators that we will use in pointers concepts
i.e.
// taking an integer variable
int number = 100;
// pointer variable ptr that stores the address of variable m
int *p = &number;
•Dereferencing operator(*) used to declare pointer variable and access the value
stored in the address.

•Address operator(&) used to returns the address of a variable or to access the


address of a variable to a pointer.
Pointer Declaration

To declare a pointer, we use the (*) dereference operator before its name. In
pointer declaration, we only declare the pointer but do not initialize it.
int * ptr;

Pointer Initialization

Pointer initialization is the process where we assign some initial value to the
pointer variable. We use the (&) addressof operator to get the memory
address of a variable and then store it in the pointer variable.
ptr = &a;

Note: We can also declare and initialize the pointer in a single step. This is
called pointer definition.

Pointer Dereferencing

Dereferencing a pointer is the process of accessing the value stored in the


memory address specified in the pointer. We use the same (*) dereferencing
operator that we used in the pointer declaration.
#include <stdio.h>

int main() {

// An integer variable
int a = 10;

// Create a pointer to integer (declaration)


int * ptr;

// Store the address of a inside pointer


(initialization)
ptr = &a;

// Print the content of ptr


printf("ptr = %p\n", ptr); Output

// Get the value pointed by ptr (dereferencing) ptr = 0x7fffa0757dd4


printf("*ptr = %d", *ptr); *ptr = 10

return 0;
}
Pointer to Pointer (Double Pointer)
In C, double pointers are those pointers which stores the address of another pointer.
The first pointer is used to store the address of the variable, and the second pointer is
used to store the address of the first pointer. That is why they are also known as
a pointer to pointer.

Syntax of Double Pointer

The syntax to use double pointer can be divided into three parts:

Declaration

A double pointer can be declared similar to a single pointer. The difference is we have
to place an additional ‘*’ before the name of the pointer.

type **name;

Above is the declaration of the double pointer with some name to the given type.
Initialization

The double pointer stores the address of another pointer to the same type.

name = &single_ptr; // After declaration


type **name = &single_ptr; // With declaration

Deferencing

To access the value pointed by double pointer, we have to use the deference operator *
two times.

*name; // Gives you the address of the single pointer


**name; // Gives you the value of the variable it points to
Example for Pointer to Pointer (Double Pointer)

#include <stdio.h>
int main() {

// A variable
int var = 10;

// Pointer to int
int *ptr1 = &var;

// Pointer to pointer (double pointer)


int **ptr2 = &ptr1;

printf("var: %d\n", var);


printf("*ptr1: %d\n", *ptr1);
printf("**ptr2: %d", **ptr2);
Output
return 0; var: 10
} *ptr1: 10
**ptr2: 10
Pointer to an Array | Array Pointer
A pointer to an array is a pointer that points to the whole array instead of the first
element of the array. It considers the whole array as a single unit instead of it being a
collection of given elements.
Syntax of Pointer to Array

type(*ptr)[size];

where,

type: Type of data that the array holds.


ptr: Name of the pointer variable.
size: Size of the array to which the pointer will
point.
For example,

int (*ptr)[10];
Here ptr is pointer that points to an array of 10 integers. Since subscript have higher
precedence than indirection, it is necessary to enclose the indirection operator and pointer
name inside parentheses.
Output
Call By Value in C

In call by value method of parameter passing, the values of actual parameters are copied to
the function’s formal parameters.
•There are two copies of parameters stored in different memory locations.

•One is the original copy and the other is the function copy.

•Any changes made inside functions are not reflected in the actual parameters of the caller.
Output

Inside swapx: x = 20 y =
10
Inside main: a = 10 b = 20
Call by Reference in C

In call by reference method of parameter passing, the address of the actual


parameters is passed to the function as the formal parameters. In C, we use pointers
to achieve call-by-reference.
•Both the actual and formal parameters refer to the same locations.

•Any changes made inside the function are actually reflected in the actual parameters
of the caller.
Output

Inside swapx: x = 20 y = 10
Inside main: a = 20 b = 10

You might also like