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

C Pointers

The document provides an overview of C pointers, including their declaration, initialization, and dereferencing. It covers various types of pointers such as null pointers, void pointers, double pointers, function pointers, and array pointers, along with examples and syntax. Additionally, it explains pointer arithmetic operations including incrementing, decrementing, and comparing pointers.

Uploaded by

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

C Pointers

The document provides an overview of C pointers, including their declaration, initialization, and dereferencing. It covers various types of pointers such as null pointers, void pointers, double pointers, function pointers, and array pointers, along with examples and syntax. Additionally, it explains pointer arithmetic operations including incrementing, decrementing, and comparing pointers.

Uploaded by

Sabari Abishek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C Pointers

Pointers (pointer variables) are special variables that are used to store addresses of another variable.
It is capable of storing the initial address of the object which is want to point to. The use of pointers
allows low-level memory access, dynamic memory allocation.

Syntax of C Pointers

datatype * ptr;

where

• ptr is the name of the pointer.

• datatype is the type of data it is pointing to.

The use of pointers in C can be divided into three steps:

1. Pointer Declaration

2. Pointer Initialization

3. Pointer Dereferencing

1. Pointer Declaration

To declare a pointer, we use the ( * ) dereference operator or indirection operator before its name.

Example

int *ptr;

2. Pointer Initialization
One way to initialize a pointer is to assign address of some variable.

Example

int var = 10;


int * ptr;
ptr = &var;

or

int *ptr = &var;

3. Pointer Dereferencing

Dereferencing a pointer means accessing the value stored at the memory address that the pointer is
pointing to. This is done using the * indirection operator in front of the pointer.

Example

int x = 10;

int *p = &x; // 'p' stores the address of variable 'x'


printf("%d", *p); // Dereferencing 'p' gives the value of 'x', i.e., 10

Types of Pointers in C

1. Null pointer
The Null Pointers are those pointers that do not point to any memory location. They can be
created by assigning a NULL value to the pointer. A pointer of any type can be assigned the
NULL value.
Syntax
data_type *pointer_name = NULL;
or
pointer_name = NULL
Example
Int *ptr = NULL;
2. Generic pointer or void pointer
It means that they do not have any associated data type. The generic can point to any type
and can be typecasted to any type. One of the main properties of void pointers is that they
cannot be dereferenced.
– We need to cast a void pointer to another kind of pointer before using it.
– Generic pointers are used when a pointer has to point to data of different types at
different times.

Syntax
void * pointer_name;
Example
Int x=5;
Void *ptr;
To print x value using pointer we need to typecast the pointer ie. *(int*)ptr
Program
#include<stdio.h>
void main()
{
Int x=5;
char a=’X’;
void *ptr;
ptr=&x;
printf(“Value of x = %d”,*(int*)ptr);
ptr=&a;
printf(“Value of a = %c”,*(char*)ptr);

3. Pointer to Pointer or Double pointer


Pointer to Pointer (double-pointers)
• To store 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.

Syntax
datatype ** pointer_name;

Example :

int **ptr

double **ptr

Initialize pointer to pointer

int x = 5;

int *ptr = &x;

int **ptr2 = &ptr;

4. Function pointers
A pointer in C is a variable that stores the address of another variable. Similarly, a variable
that stores the address of a function is called a function pointer or a pointer to a function.
Function pointers can be useful when you want to call a function dynamically.

Declare syntax
function_return_type(*Pointer_name)(function argument list)

Example

#include <stdio.h>
int addition (int a, int b)
{
return a + b;
}
int main()
{
int (*ptr)(int, int) = addition;
int x = 10, y = 20;
int z = (*ptr)(x, y);
printf("Addition of x: %d and y: %d = %d", x, y, z);
return 0;
}

5. Array pointer
An array pointer is a pointer that points to the first element of an array. It allows you to
access array elements indirectly through pointer arithmetic or dereferencing.

Syntax

data_type (*var_name)[size_of_array];

Here:

• data_type is the type of data that the array holds.

• var_name is the name of the pointer variable.


• size_of_array is the size of the array to which the pointer will point.

Example

int (*ptr)[10];

(OR)

int arr[5] = {1, 2, 3, 4, 5};


int *ptr = arr; // or equivalently, int *ptr = &arr[0];

Program

#include <stdio.h>

int main()

int arr[5] = {10, 20, 30,40,50};

int *ptr = arr; // ptr points to arr[0]

for (i = 0; i < 5; i++)

printf("%d\n", *(ptr + i));


}

return 0;

OUPUT

printing all the elements in array using pointer

10
20
30
40
50
Pointer Arithmetic
Pointers (pointer variables) are special variables that are used to store addresses of another
variable.

C pointers arithmetic operations are different from the general arithmetic operations. The
following are some of the important pointer arithmetic operations in C:

• Increment and Decrement of a Pointer

• Addition and Subtraction of Integer to Pointer

• Subtraction of Pointers

• Comparison of Pointers

1. Increment and Decrement of a Pointer


Increment: It is a condition that also comes under addition. When a pointer is incremented, it actually
increments by the number equal to the size of the data type for which it is a pointer.

For Example:
If an integer pointer that stores address 1000 is incremented, then it will increment by 4(size
of an int), and the new address will point to 1004. While if a float type pointer is incremented
then it will increment by 4(size of a float) and the new address will be 1004.

Decrement: It is a condition that also comes under subtraction. When a pointer is decremented, it
actually decrements by the number equal to the size of the data type for which it is a pointer.
For Example:

If an integer pointer that stores address 1000 is decremented, then it will decrement by 4(size
of an int), and the new address will point to 996. While if a float type pointer is decremented
then it will decrement by 4(size of a float) and the new address will be 996.
2. Addition and subtraction of Integer to Pointer
When a pointer is added or subtracted with an integer value, the value is first multiplied by
the size of the data type and then added or subtracted to the pointer.
For Example: (Addition)
Consider the same example as above where the ptr is an integer pointer that stores 1000 as an
address. If we add integer 5 to it using the expression, ptr = ptr + 5, then, the final address
stored in the ptr will be ptr = 1000 + sizeof(int) * 5 = 1020.

3. Subtraction of Two Pointers


The subtraction of two pointers is possible only when they have the same data type. The result
is generated by calculating the difference between the addresses of the two pointers and
calculating how many bits of data it is according to the pointer data type.

For Example:
Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are subtracted. The difference
between addresses is 4 bytes. Since the size of int is 4 bytes, therefore the increment between ptr1
and ptr2 is given by (4/4) = 1.

4. Comparison of Pointers
We can compare the two pointers by using the comparison operators in C. We can implement this
by using all operators in C >, >=, <, <=, ==, !=. It returns true for the valid condition and returns
false for the unsatisfied condition.

Example program
#include <stdio.h>
int main()
{
int a = 22;
int *p = &a;
// pointer increment and decrement
printf("p = %u\n", p);
p++;
printf("p++ = %u\n", p);
p--;
printf("p-- = %u\n", p);

int N = 4, X=6;
int *ptr1 = &N;
int *ptr2 = &X;
// Addition of 3 to ptr1
printf("Pointer ptr1 after Addition: ");
printf("%u \n", ptr1);
Ptr1 = ptr1 + 3;
printf("Pointer ptr1 after Addition: ");
printf("%u \n", ptr1);
// pointer to pointer subtraction
ptr2 = &X; ptr1 = &N;
x = ptr2 - ptr1;
printf("Subtraction of ptr1 “& ptr2 is %d\n", x);
}

OUTPUT
p = 6422308
p++ = 6422312
p-- = 6422308
Pointer ptr1 before Addition: 6422308
Pointer ptr1 after Addition: 6422320
Subtraction of ptr1 & ptr2 is 1

You might also like