How can I set an array's size at run time?

Answer Posted / nithin

int main()
{
int i, max;

// Array
int *a;

printf("Enter the size of array\n");
scanf("%d", &max);

//Run time size allocation of an array
a = (int*)malloc(max * sizeof(int));

for(i = 0; i < max;i++)
{
a[i] = i * 2;
printf("value of element %d is %d\n", i, a
[i]);
}
return 0;
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

#include main() { int *p, *c, i; i = 5; p = (int*) (malloc(sizeof(i))); printf(" %d",*p); *p = 10; printf(" %d %d",i,*p); c = (int*) calloc(2); printf(" %d ",*c); }

620


Explain what does it mean when a pointer is used in an if statement?

611


How can I remove the trailing spaces from a string?

603


Where in memory are my variables stored?

623


Can you apply link and association interchangeably?

662






What is getch?

624


What is the explanation for prototype function in c?

559


What is the difference between mpi and openmp?

724


What is the value of h?

583


How to delete a node from linked list w/o using collectons?

2081


What do you mean by command line argument?

634


Explain how can I manipulate strings of multibyte characters?

772


What is pass by reference in c?

603


What is operator promotion?

617


what is the structure pointer?

1637