I want the length of this array to be taken as input from user.
let n be the entered length;
when I use malloc: int *arr1[n] = malloc((sizeof(int) * n) + 1);
it says a variable sized array can't be initialized, is there any other way to do it?
I want to take an array as input from the user, sort the elements of the array and store the sorted elements in another array. Here's my complete code:
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b)
{
int temp = 0;
temp = *a;
*a = *b;
*b = temp;
}
int main(void)
{
int n = 0;
printf("Enter the number of elements - ");
scanf("%i", &n);
int *arr1[n];
int arr2[] = malloc((sizeof(int) * n) + 1);
printf("Enter the elements one by one - ");
for (int i = 0; i < n; i++)
{
scanf("%i", arr1[i]);
}
free(arr2);
}
I didn't declare the second array as arr2[n] as n is variable and it won't let me initialize the array with malloc. Can someone please help me with this?
+ 1? And what isarr2for? You don't use it at all?%iand%din scanf