• Articles
  • Tutorials
  • Interview Questions
  • Webinars

Dynamic Memory Allocation in C

What are Pointers in C

It is a variable which is used to store the address of another variable.

pointer

Advantages of pointer

  • Return multiple values from function
  • Access any memory location
  • Improves the performance
  • Reduces the code
  • Used for dynamic memory allocation
  • Used in arrays, functions and structures
Symbol   Name    Description
& address of operator Determines the address of a variable.
* indirection operator Accesses the value at the address.

Example

#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int n=50;
int *p;    //declaration of pointer
p=&n; //stores the address of number variable
printf("Address of n variable is %x n",&n);
printf("Address of p variable is %x n",p);
printf("Value of p variable is %d n",*p);
getch();
}

Output
Address of n variable is fff4
Address of p variable is fff4
Value of p variable is 50

Certification in Full Stack Web Development

Master File Handling in C with our comprehensive tutorial!

Dynamic memory allocation in C

Dynamic memory allocation means to allocate the memory at run time. Dynamic memory allocation is possible by 4 functions of stdlib.h header file.

  1. malloc()
  2. calloc()
  3. realloc()
  4. free()
malloc() Allocates single block of requested memory.
calloc() Allocates multiple block of requested memory.
realloc() Reallocates the memory occupied by malloc() or calloc() functions.
free() Frees the dynamically allocated memory.

Course Schedule

Name Date Details
Python Course 20 Jul 2024(Sat-Sun) Weekend Batch
View Details
Python Course 27 Jul 2024(Sat-Sun) Weekend Batch
View Details
Python Course 03 Aug 2024(Sat-Sun) Weekend Batch
View Details

About the Author

Senior Consultant Analytics & Data Science

Presenting Sahil Mattoo, a Senior Consultant Analytics & Data Science at Eli Lilly and Company is an accomplished professional with 14 years of experience across data science, analytics, and technical leadership domains, demonstrates a remarkable ability to drive business insights. Sahil holds a Post Graduate Program in Business Analytics and Business Intelligence from Great Lakes Institute of Management.