-
-
Save Irene-123/eeb098ad510b69da82c7ca98b880680e to your computer and use it in GitHub Desktop.
Dynamically allocating space for a 1D array in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdlib.h> | |
#include<stdio.h> | |
int main(){ | |
int n; | |
int *arr; | |
printf("\nEnter the number of elements in the array:" ); | |
scanf("%d",&n); | |
arr=(int*)malloc(n*sizeof(n)); | |
if (arr==NULL){ | |
printf("\nThe space could not be allocated:"); | |
return (0); | |
} | |
printf("\nEnter the values of the array"); | |
for (int i=0;i<n;i++){ | |
scanf("%d",&arr[i]); | |
} | |
printf("\n"); | |
for (int i=0;i<n;i++){ | |
printf("%d\t", arr[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment