





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






A pointer is a specialized variable designed to store the memory address of another variable. It provides the ability to manipulate memory addresses directly and is fundamental in many programming constructs, including arrays, structures, and dynamic memory allocation. Why Use Pointers? Pointers offer the following advantages: Call by Reference: Allow functions to modify variables directly. Efficient Memory Usage: Handle arrays and structures more efficiently. Dynamic Data Structures: Enable the creation of linked lists, trees, graphs, etc. Dynamic Memory Allocation: Manage memory on the heap dynamically. Function Tables: Support event handling in systems like GUIs and signal processing. Input Functions: Simplify operations like scanf. However, pointers lack built-in safety features, and improper usage can lead to memory leaks or crashes. Basic Concepts Declaring and Using Pointers A pointer is declared using the * operator: int *p; // p is a pointer to an integer Example usage: int a = 5; int *p = &a; // Pointer p stores the address of a *p = 10; // Modifying the value at the address printf("%d", a); // Output: 10 int *ptr; //Here ptr is Integer PointerVariable int ptr; //Here ptr is Normal Integer Variable Pointer Overview: int i; int *j; j=&i;
Explanation: i is the name given for particular memory location of ordinary variable. Let us consider its Corresponding address be 65624 and the Value stored in variable „i‟ is 5 The address of the variable “i” is stored in another integer variable whose name is “j” and which is having corresponding address 65522 j = &i; i.e j = Address of i Here j is not ordinary variable, It is special variable and called pointer variable as it stores the address of the another ordinary variable. Key Pointer Operations Address (&) and Dereference () Operators* &: Provides the address of a variable. *: Accesses the value at the pointer's address. Pointer Basic Example: #include<stdio.h> int main() { int ptr, i; i = 11; / address of i is assigned to ptr / ptr = &i; / show i's value using ptr variable */ printf("Value of i : %d", *ptr); return 0; } Output: Value of i = 11 Pointer information: When we declare integer pointer then we can only store address of integer variable into that pointer. Similarly, if we declare character pointer then only the address of character variable is stored into the pointer variable. Simple Pointer Example #1: #include<stdio.h> int main() {
Pointers are used to store the address of other variables of similar datatype. But if you want to store the address of a pointer variable, then you again need a pointer to store it. Thus, when one pointer variable stores the address of another pointer variable, it is known as Pointer-to- Pointer variable or Double Pointer. Syntax: int *p1; Here, we have used two indirection operator() which stores and points to the address of a pointer variable i.e, int *. If we want to store the address of this (double pointer) variable p1, then the syntax would become: int *p2; Simple program to represent Pointer to a Pointer #include<stdio.h> int main () { int a =10; int p1; //this can store the address of variable a intp2; / this can store the address of pointer variable p1 only. It cannot store the address of variable 'a' */ p1 = &a; p2 = &p1; printf("Address of a = %u\n", &a); printf("Address of p1 = %u\n", &p1); printf("Address of p2 = %u\n\n", &p2); // below print statement will give the address of 'a' printf("Value at the address stored by p2 = %u\n", *p2); printf("Value at the address stored by p1 = %d\n\n", *p1); printf("Value of **p2 = %d\n", *p2); //read this (p2) / This is not allowed, it will give a compile time error- p2 = &a; printf("%u", p2); */ Return 0; } Output: Address of a = 2686724 Address of p1 =
Address of p2 = Value at the address stored by p2 = 2686724 Value at the address stored by p1 = 10 Value of **p2 =
A Generic pointer is a special pointer that can point to object of any type. A Generic pointer is type less pointer also known as void pointer. void pointer is an approach towards generic functions and generic programming in C. Note: Writing programs without being constrained by data type is known as generic programming. A generic function is a special function that focuses on logic without confining to data type. For Example, logic to insert values in array is common for all types and hence can be generic function. Syntax to declare void or Generic pointer void * pointer-name; Example to declare void or Generic pointer void * vPtr; How to dereference a void or Generic pointer Dereferencing is the process of retrieving data from memory location pointed by a pointer. It converts block of raw memory bytes to a meaningful data (data is meaningful if type is associated). While dereferencing a void or Generic pointer, the C compiler does not have any clue about type of value pointed by the void pointer. Hence, dereferencing a void pointer is illegal in C. But a pointer will become useless if you cannot dereference it back. To dereference a void pointer, you must typecast it to a valid pointer type. Example to dereference a void or Generic pointer int num = 10; void * vPtr = # // void pointer pointing at num int value = *((int *) vPtr); // Dereferencing void pointer Pointers and Arrays Pointers and arrays share a close relationship: An array name acts as a constant pointer to its first element.
Although, C language inherently does not have any technique to allocated memory dynamically, there are 4 library functions under "stdlib.h" for dynamic memory allocation. Dynamic memory management refers to manual memory management. This allows you to obtain more memory when required and release it when not necessary. C provides functions to allocate and manage memory dynamically:
Write a C program to find sum of n elements entered by user. To perform this program, allocate memory dynamically using malloc () function. #include<stdio.h> #include<stdlib.h> int main () { int n,i,ptr,sum=0; printf("Enter number of elements: "); scanf("%d",&n); ptr=(int) malloc(nsizeof(int)); //memory allocated using malloc if(ptr==NULL) { printf("Error! memory not allocated."); if(ptr==NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements of array: "); for(i=0;i<n;i++) { scanf(“%d”,ptr+i); sum+=(ptr+i); } printf(“sum=%d”,sum); free(ptr); return 0; }
In static memory allocation, memory is allocated while writing the C program. Actually, user requested memory will be allocated at compile time. In dynamic memory allocation, memory is allocated while executing the program. That means at run time. Memory size can’t be modified while execution. Example: array Memory size can be modified while execution. Example: Linked list Difference Between malloc () and calloc() Functions:
It allocates only single block of requested memory It allocates multiple blocks of requested memory int ptr; ptr = malloc (20 * sizeof(int)); For the above, 204 bytes of memory only allocated in one block. Total = 80 bytes int ptr; Ptr = calloc( 20, 20 * sizeof(int) ); For the above, 20 blocks of memory will be created and each contains 204 bytes of memory. Total = 1600 bytes