Malloc

PHOTO EMBED

Sun May 14 2023 08:13:35 GMT+0000 (Coordinated Universal Time)

Saved by @prachi

#include<stdio.h>
#include<stdlib.h>
int main(){
  int *arr;
  int i,n;
  printf("Enter no of elements:");
  scanf("%d",&n);
  printf("Entered no is:%d",n);
  arr = (int*)malloc(n * sizeof(int));
  if(arr == NULL){
    printf("\nMemory not allocated");
    exit(0);
  }
  else{
    printf("\nMemory successfully allocated using malloc");
    for(i=0;i<n;++i){
      arr[i] = i + 1;
    }
    printf("\nElement of array are: ");
    for(i=0;i<n;++i){
      printf("%d,",arr[i]);
    }
    printf("\nAddress of array are: ");
    for(i=0;i<n;++i){
      printf("%d,",&arr[i]);
    }
  }
  return 0;
}
content_copyCOPY