# C PROGRAMS
Pointers Examples
➤ Pointer Basic Examples
➤ Dynamic Memory Allocation
➤ Read Write Array with Pointer
➤ Sum Avg of Array with Pointer
➤ Sort Array with Pointer
➤ Search in Array using Pointer
➤ Sum of N Numbers
➤ Largest Number by DMA
Others
➤ C Program Without Main()
➤ Hello World Without ;
➤ Process & Parent Process ID
➤ C Program Without Header File
➤ void main(), main() vs int main()
➤ fork() function in C
➤ Why gets function is dangerous
➤ Undefined reference to sqrt
Dynamic Memory Allocation in C | There are 4 library functions defined under for dynamic memory allocation in C programming.
- malloc() :- Allocates requested size of bytes and returns a pointer first byte of allocated space.
- calloc() :- Allocates space for an array element, initializes to zero and then returns a pointer to memory.
- free() :- Deallocate the previously allocated space.
- realloc() :- Change the size of previously allocated space.
Malloc() used to allocate memory to structures (user-defined data types).
On successful allocation, it returns the base address of memory block. On failure, it returns the NULL pointer.
#include<stdio.h>
#include<stdlib.h>
struct student
{
int id;
char name[20];
float fee;
};
struct student *ptr;
int main()
{
ptr=(struct student*) malloc(sizeof(struct student));
/*check memory is allocated or not*/
if(ptr==NULL)
{
printf("Memory not allocated\n");
exit(1);
}
else
{
printf("Enter Student details (Student Id, Name & fee):\n");
scanf("%d,%s,%f",&ptr->id,ptr->name,&ptr->fee);
printf("\n\nStudent details is:\n");
printf("%d,%s,%.2f",ptr->id,ptr->name,ptr->fee);
}
return 0;
}
Output:-
Enter Student details (Student Id, Name & fee):
1204,amelia,1205.2
Student details is:
1204,amelia,1205.02
Calloc() allocates memory to arrays. On success, it allocates ( n * size ) bytes and returns base address. On failure, it returns the NULL pointer.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, i, *arr;
printf("Enter size of array: ");
scanf("%d",&n);
/*allocate memory using calloc()*/
arr= (int*) calloc(n, sizeof(int));
/*check memory is allocated or not*/
if(arr==NULL)
{
printf("Memory not allocated.");
exit(0);
}
else
{
printf("Array elements are:\n");
for(i=0; i<n; i++)
{
printf("%d\t",*(arr+i));
}
}
return 0;
}
Output:-
Enter size of array: 3
Array elements are:
0 0 0
Notice:- when we initialize memory dynamically with calloc() than all elements of array initialize with “0“.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Some Examples C programming
- Find the sum of n elements entered by the user
- Find Largest Number Using Dynamic memory allocation
- C program to find the sum of elements in an array
- Display even and odd numbers in an array
- Sum and count of even and odd numbers in an array
- Count the positive, negative, and zeros in an array
- Sum of positive and negative numbers in an array
- Average and numbers greater than average in array
- Smallest & largest array element with their position
- First max and min, Second max and min number in array
- C program to sort a list of an array element & display
- C program to search an Array element and display index
- Search position of Nth times occurred element in array
- C program to insert an element in a given array
- C program to delete an element in a given array