POINTERS
Pointers are powerful features of C and (C++) programming that differentiates it from other popular programming languages like: Java and Python.
Pointers are used in C program to access the memory and manipulate the address.
If you have a variable var in your program, &var
will give you its address in the memory, where &
is commonly called the reference operator.
You must have seen this notation while using scanf() function. It was used in the function to store the user inputted value in the address of var.
scanf("%d", &var);
Example to demonstrate use of reference operator in C programming.
/ * ********************************************************** * /
/ / ************************************************************
# include < stdio.h >
# include < conio.h >
int main ( )
{
int var = 5 ;
clrscr ( ) ;
Printf ( " value is % d \n " , var ) ;
Printf ( " address is % u \n " , & var ) ;
return 0 ;
}
Pointer variables
n C, there is a special variable that stores just the address of another variable. It is called Pointer variable or, simply, a pointer.
Declaration of Pointer
datatype* pointer_variable_name;
int* p;
Above statement defines, p as pointer variable of type int
.
Reference operator (&) and Dereference operator (*)
As discussed, & is called reference operator. It gives you the address of a variable.
Likewise, there is another operator that gets you the value from the address, it is called a dereference operator (*).
Below example clearly demonstrates the use of pointers, reference operator and dereference operator.
Note: The * sign when declaring a pointer is not a dereference operator. It is just a similar notation that creates a pointer.
Example To Demonstrate Working of Pointers
/* Source code to demonstrate, handling of pointers in C program */
/ ****************************************************************
#include
< stdio.h >
#include
< conio.h >
int main()
{
int* pc;
int c;
c=22;
clrscr ( ) ;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
return 0;
}
Pointers and Arrays
Arrays are closely related to pointers in C programming but the important difference between them is that, a pointer variable takes different addresses as value whereas, in case of array it is fixed.
This can be demonstrated by an example:
/* *************************************** */
/ ****************************************************************
#include
< stdio.h >
#include
< conio.h >
int main()
{
char charArr [ 4 ] ;
int i ;
clrscr ( ) ;
for( i = 0; i < 4; ++i )
{
printf ( " Address of charArr [ %d ] = %u \ n " , i, & charArr[i] ) ;
}
return 0 ;
}
1.Program to find the sum of six numbers with arrays and pointers.
1.Program to find the sum of six numbers with arrays and pointers.
/* *************************************** */
/ ****************************************************************
#include
< stdio.h >
#include
< conio.h >
int main()
{
int i, classes [ 6 ] , sum = 0 ;
clrscr ( ) ;
printf ( " Enter 6 numbers : \ n " ) ;
for ( i = 0 ; i < 6 ; ++ i ){
// (classes + i) is equivalent to &classes [ i ]
// *(classes + i) is equivalent to classes [ i ]
sum = sum + *( classes + i ) ;
}
printf ( " Sum = %d " , sum ) ;
return 0 ;
}
Pointers and Functions
Program to swap two number using call by reference.
/* C Program to swap two numbers using pointers and function */
//****************************************
# include < stdio.h >
# include < conio.h >
void swap( int *n1, int *n2 );
int main ( )
{
int num1 = 5, num2 = 10;
// address of num1 and num2 is passed to the swap function
swap( &num1, &num2);
printf ( " Number 1 = % d \ n ", num1 ) ;
printf ( " Number 2 = % d ", num 2 ) ;
return 0 ;
}
void swap ( int * n1, int * n2 )
{
// pointer n1 and n2 points to the address of num1 and num2 respectively
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
C Dynamic Memory Allocation
In this article, you'll learn to dynamically allocate memory in your C program using standard library functions: malloc(), calloc(), free() and realloc()
As you know, you have to declare the size of an array before you use it. Hence, the array you declared may be insufficient or more than required to hold data. To solve this issue, you can allocate memory dynamically.
Dynamic memory management refers to manual memory management. This allows you to obtain more memory when required and release it when not necessary.
Although C inherently does not have any technique to allocate memory dynamically, there are 4 library functions defined under
Function | Use of Function |
---|---|
malloc() | Allocates requested size of bytes and returns a pointer first byte of allocated space |
calloc() | Allocates space for an array elements, initializes to zero and then returns a pointer to memory |
free() | deallocate the previously allocated space |
realloc() | Change the size of previously allocated space |
C malloc()
The name malloc stands for "memory allocation".
The function malloc() reserves a block of memory of specified size and return a pointer of type void which can be casted into pointer of any form.
Syntax of malloc()
ptr = (cast-type*) malloc(byte-size)
Here, ptr is pointer of cast-type. The malloc() function returns a pointer to an area of memory with size of byte size. If the space is insufficient, allocation fails and returns NULL pointer.
ptr = (int*) malloc(100 * sizeof(int));
This statement will allocate either 200 or 400 according to size of int 2 or 4 bytes respectively and the pointer points to the address of first byte of memory.
C calloc()
The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.
Syntax of calloc()
ptr = (cast-type*)calloc(n, element-size);
This statement will allocate contiguous space in memory for an array of n elements. For example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for an array of 25 elements each of size of float, i.e, 4 bytes.
C free()
Dynamically allocated memory created with either calloc() or malloc() doesn't get freed on its own. You must explicitly use free() to release the space.
syntax of free()
free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
Example : Using C malloc() and free()
Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using malloc() function.
Example 2: Using C calloc() and free()
Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using calloc() function.
C realloc()
If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc().
Syntax of realloc()
ptr = realloc(ptr, newsize);
Here, ptr is reallocated with size of newsize.
Example 3: Using realloc()