Pointers in C Language
Pointers in C Language
1. Concepts of pointers
2. Pointer operations
3. Special pointers
4. Application of pointers
5. Memory allocations in C
1. Concept of Pointers
• A pointer is a variable that stores the memory address
of another variable.
• The general syntax of declaring a pointer:
data_type* ptr; // declare pointer ptr of data_type
data_type x ;
ptr = &x; // assign address of x to ptr
The * informs the compiler that ptr is a pointer type
variable and data_type specifies that it will store the
address of a data_type type variable.
We say ptr points to x, or points to memory location of x
Dereferencing a pointer is to get the value stored
the at memory pointed by the pointer. * is the
dereference operator.
*ptr gets the value at the memory address of ptr
Example
int *p;
*p = 10;
// this is not correct, because p does not point to any valid address
#include<stdio.h>
int main() {
int x = 1890259661;
int *ptr = &x;
printf("Value of x is %d\n", x);
printf("Runtime memory address of x is %lu\n", (unsigned long int) &x);
printf("Value of pointer ptr is %lu\n", (unsigned long int) ptr);
printf("Size of pointer ptr is %d\n", sizeof(ptr));
printf("Runtime memory address of pointer ptr is %lu\n", (unsigned long int) &ptr);
printf("Value of *ptr is %d\n", *ptr);
*ptr = 10;
printf("Value of *ptr is %d\n", *ptr);
printf("Value of x is %d\n", x);
return 0;
} Value of x is 1890259661
Runtime memory address of x is 6684316
Value of pointer ptr is 6684316
Size of pointer ptr is 4
Runtime memory address of pointer ptr is 6684312
Value of *ptr is 1890259661
Value of *ptr is 10
Value of x is 10
1890259661 | 10 = 01110000 10101011 00010010 11001101 | 2
memory block of x
x’s value is 1890259661
Pointer is a tool to access data
Variable Pointer
int x = 10; int *p; p is declared to be an int pointer
&x represents address of x p = &x; p holds address of x
x represents the value stored at *p represents the value stored at p, i.e. x
&x int b = *p; b will have value 10.
*p = 20; x will have value 20.
2. Comparisons
Value of a pointer is an address of integer value. We can compare pointers by
using relational operators in the expressions. For example,
p1 > p2 , p1==p2, p1!=p2 are all valid in C.
3. Add and minus operations
Adding/substracting an integer value c to/from a data_type pointer will
increase/decrease the address by c * sizef(data_type).
Example:
int x, *p = &x; p = p + 2;
p’s value will be increased by 2*sizeof(int) = 2*4 = 8. e.g. if p’s value is
1000, then after p = p+2; p’s value becomes 1008.
process(a, b, min); // 2
printf("\n");
return 0;
}
4. Applications of pointers
1. Pointers are used in pass-by-references. This
enables to input/output multiple values to/from
a function through function arguments.
2. Pointer increases the efficiency of accessing data
for algorithms.
3. Pointers are used in dynamic memory allocation
and management.
4. Pointers are used to implement complex data
structures like linked lists, queues, stacks, trees.
5. Memory allocations in C
1. Static memory allocation: memory allocation done by declaration
of global or static variables. At compile time, a global or static
variable is allocated a memory block with a relative address to the
data region. At runtime, the statically allocated memory blocks are
instanced with absolute addresses in the data region. The size of the
data region is determined by the sum of the sizes of statically
allocated memory blocks at compile time, it won’t be changed and
released at runtime.