Skip to content

Instantly share code, notes, and snippets.

@Irene-123
Created January 3, 2021 13:51
Show Gist options
  • Save Irene-123/eeb098ad510b69da82c7ca98b880680e to your computer and use it in GitHub Desktop.
Save Irene-123/eeb098ad510b69da82c7ca98b880680e to your computer and use it in GitHub Desktop.
Dynamically allocating space for a 1D array in C
#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