Pointers
Pointers
POINTERS
What will you learn?
Pointers
Pointer Arithmetic
Constant Pointer
Generic Pointer
Pointer to Pointer
Arrays and Pointers
Pointer to Functions
Dynamic Memory Allocation
WHY POINTER?
Pointer in c are used for several reasons, offering
flexibility and control over memory and data. Here some key
reasons for using pointers.
Function Arguments
Syntax: -
data-type *pointer_name;
Datatype: The type of the data that the pointer will point to (e.g.,
float a;
int *ptr;
ptr = &a; // ERROR, type mismatch
USING THE POINTER (OR) DEREFERENCING
POINTER
A Dereferenced pointer is what you get when you access
or modify the value at the memory address stored by the
pointer.
int a = 10;
Note: -
int main()
return 0;
Syntax: -
Note: -
int main()
if (ptr == NULL)
return 0;
}
GENERIC POINTER
A generic pointer is a pointer that can point to any data
type.
A void pointer can hold address of any type and can be type-
casted to any type(int, char, float, etc.)
Syntax: -
void *ptr;
Here ptr is a pointer to void, which means it can point to any data
type.
CHARACTERISTICS OF GENERIC
POINTER
1. A generic pointer declared as void*.
char c = 'A';
return 0;
}
CONSTANT POINTER
A constant pointer(also known as “pointer constant”) is declared
with the const keyword after the *.
Syntax: -
Here *const makes the pointer constant, meaning it can only point
to the address it is initialized with.
EXAMPLE PROGRAM FOR CONSTANT
POINTER
#include <stdio.h> output: - Value of a: 10
int a = 10;
int b = 20;
*ptr = 15; // Allowed: Changing the value at the memory location 'a'
return 0;
}
DIFFERENCE BETWEEN CONSTANT
POINTER AND POINTER TO CONSTANT
Constant pointer(data_type *const ptr): The pointer
address cannot change, but the value it points to can be
modified.
The following are the operations that can be performed over pointers.
3. We can compare two pointers if they point to the elements of the same array by
using relational operators.(<, >, == etc.)
4. We can subtract one pointer from another pointer if both point to the same array.
5. We can assign one pointer to another to another pointer provided both are of
same type.
BUT THE FOLLOWING OPERATIONS
ARE NOT POSSIBLE:
return 0; }
POINTER TO A
POINTER
Pointer is also a variable, it is also allocated with memory space
and its address also is retrievable.
Syntax:
data-type **variable;
3. The base address is the location of the first element of the array
denoted by a[0].
Syntax: -
data_type *array_name[size];
EXAMPLE: -
int a [5] = {1, 2, 3, 4, 5};
Here if the base address is 1000 for “a” and if integer occupies 4
bytes then the five elements requires 20 bytes as shown below.
p = &a[0]; P1 P2
int X=P2 – P1
Now it is possible to access every value of a using p++ to move from one element to
another element as
P+1 is equal to &a[1] i.e. 1002 and *(p+1) gives value at address 1002 i.e. 2
P+2 is equal to &a[2] i.e. 1004 and *(P+2) gives value at address 1004 i.e. 3
P+3 is equal to &a[3] i.e. 1006 and *(P+3) gives value at address 1006 i.e. 4
P+4 is equal to &a[4] i.e. 1008 and *(P+4) gives value at address 1008 i.e. 5
CONTINUED.,
In one dimensional array “a” the expression, *(a+i) or *(p+i)
represents ith element in array.
an element in a two dimensional array can be represented by the
pointer expression as *(*(a+i)+j) or *(*(p+i)+j)
#include <stdio.h>
int main()
int arr[3]={20,30,40};
int *p;
return 0;
}
EXAMPLE PROGRAM ARRAY OF POINTERS TO
INTEGERS
#include <stdio.h>
int main() {
int *arr[3];
arr[0] = &a;
arr[1] = &b;
arr[2] = &c;
return 0;
}
POINTERS TO
FUNCTIONS
POINTERS TO
FUNCTIONS
1. In C, pointers to functions are pointers that store the address
of a function.
1. malloc()
2. calloc()
3. realloc()
4. free()
MALLOC()
malloc( ) function is used to allocate space in memory during the execution
of the program.
Syntax: -
int *ptr;
if (ptr == NULL)
It allocates storage for multiple blocks and initializes them all to zero
and the pointer will point to the first byte of the first block. Here all
blocks are same size.
Syntax: -
if (ptr == NULL)
}
REALLOC()
Realloc() stands for reallocation.
Syntax:
int *ptr;
if (ptr == NULL)
printf("Reallocation failed\n");
}
FREE()
Free() is used to deallocate memory that previously
allocated by malloc(), calloc(), or realloc().
Syntax : -
int *ptr;
57
#include <stdio.h>
int main() {
int x = 10, *y;
y = &x;
*y = *y + 5;
printf("%d", x);
return 0;
}
58
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
int *ptr = arr;
ptr++;
printf("%d", *ptr);
return 0;
}
59
Which of the following statements about C pointers is
incorrect?
(a) Pointers can be used to store the address of variables.
(b) Pointer arithmetic is allowed in C.
(c) A pointer can be dereferenced to access the value at the
memory location it points to.
(d) You can assign the address of a constant directly to a
pointer and modify the value of the constant.
Answer:
60
#include <stdio.h>
int main() {
int a = 5, b = 10;
int *p = &a, *q = &b;
*p = *q;
printf("%d %d", a, b);
return 0;
}
61