malloc
- Include the necessary header file for the malloc function: #include
- Declare a pointer variable to store the memory address returned by malloc: int *ptr;
- Use malloc to allocate a block of memory for the pointer variable: ptr = (int)malloc(10 sizeof(int));
- Check if the memory allocation was successful: if (ptr == NULL) { printf("Memory allocation failed"); exit(1); }
- Use the allocated memory for the intended purpose.
- Free the allocated memory when it is no longer needed: free(ptr);