
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is a malloc function in C language?
The malloc() function stands for memory allocation, that allocate a block of memory dynamically.
It reserves the memory space for a specified size and returns the null pointer, which points to the memory location.
malloc() function carries garbage value. The pointer returned is of type void.
The syntax for malloc() function is as follows −
ptr = (castType*) malloc(size);
Example
The following example shows the usage of malloc() function.
#include<stdio.h> #include<string.h> #include<stdlib.h> int main(){ char *MemoryAlloc; /* memory allocated dynamically */ MemoryAlloc = malloc( 15 * sizeof(char) ); if(MemoryAlloc== NULL ){ printf("Couldn't able to allocate requested memory
"); }else{ strcpy( MemoryAlloc,"TutorialsPoint"); } printf("Dynamically allocated memory content : %s
", MemoryAlloc); free(MemoryAlloc); }
Output
When the above program is executed, it produces the following result −
Dynamically allocated memory content: TutorialsPoint
- Related Articles
- What is malloc in C language?
- Explain malloc function in C programming
- What is strlen function in C language?
- What is strcoll() Function in C language?
- What is strspn() Function in C language?
- What is strcpy() Function in C language?
- What is strcat() Function in C language?
- What is strncat() Function in C language?
- What is strcmp() Function in C language?
- What is strncmp() Function in C language?
- What is strstr() Function in C language?
- What is strncpy() Function in C language?
- What is strrev() Function in C language?
- What is function prototype in C language
- What is exit() function in C language?

Advertisements