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

In C, What is the #line used for?

1545


Explain built-in function?

865


When do you not use the keyword 'return' when defining a function a) Always b) Never c) When the function returns void d) dfd

849


What is sizeof in c?

775


What is d scanf?

805


Explain how do you determine a file’s attributes?

833


A program is required to print your biographic information including: Names, gender, student Number, Cell Number, line of study and your residential address.

1486


How can this be legal c?

851


Is c is a high level language?

873


How can I remove the trailing spaces from a string?

839


Write a code on reverse string and its complexity.

799


how to create duplicate link list using C???

2286


What are the application of void data type in c?

981


What is 'bus error'?

880


How do you view the path?

902