#define n 10
int a[n];
I wanted to declare an array globally and modify the size in other function
int main(){
int b;
printf("Enter the number of elements\n");
scanf("%d",&b);
#undef n
#define n b
for(int i = 0;i < n; i++)
scanf("%d",&a[i]);
display();
}
I modified the size in the main function
void display()
{
for(int i=0;i<n;i++)
printf("%d ",a[i]);
}
when i enter a size like 5 and enter the elements 1 2 3 4 5 the output shows 5 elements followed by 5 zeroes 1 2 3 4 5 0 0 0 0 0 how to remove the zeroes?
size, that keeps track of the number of elements in the array, and when you diplay it, instead of looping from0ton, loop from0tosize.#defineworks... The preprocessor works just by brutal text substitution, so it's not like changing a#definecan magically affect a section of text that has already been processed.mallocis unnecessary. Thereallocusage is wrong and it is not a complete example.