Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*this program tells us how to dynamically allocate memory for 2d matrix*/
- /*memory allocated by calloc has initial value '0'*/
- #include<stdio.h>
- #include<malloc.h>
- int main()
- { int n=3,i,j;
- int *ptr;
- ptr= calloc(n*n, sizeof(int));
- /*to get input from user*/
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- {
- printf("Enter the value %d: ",i*n +j+1);
- scanf("%d",&ptr[i*n + j]);
- }
- // printf("\n");
- }
- /*Now display the dynamically allocated 2d matrix*/
- printf("\nHere is your matrix: \n");
- for(i=0;i<n;i++)
- {
- for(j=0;j<n;j++)
- printf("%d ",ptr[i*n + j]);
- printf("\n");
- }
- free(ptr);
- return 0;
- }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement