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

How can you check to see whether a symbol is defined?

596


How does sizeof know array size?

634


while loop contains parts a) initialisation, evalution of an expression,increment /decrement b) initialisation, increment/decrement c) condition evalution d) none of the above

750


Why c is a mother language?

560


what is recursion in C

619






Why shouldn’t I start variable names with underscores?

629


What is the function of multilevel pointer in c?

674


Explain the difference between malloc() and calloc() in c?

581


What is double pointer?

563


What are derived data types in c?

618


Explain a file operation in C with an example.

665


if a is an integer variable, a=5/2; will return a value a) 2.5 b) 3 c) 2 d) 0

1456


An integer that indentifies the position of a data item in a sequence of data items a) value b) number c) index d) all of the above

651


The program will first compute the tax you owe based on your income. User is prompted to enter income. Program will compute the total amount of tax owed based on the following: Income Tax 0 - $45,000 = 0.15 x income $45,001 - $90,000 = 6750 + 0.20 x (income – 45000) $90,001 - $140,000 = 15750 + 0.26 x (income – 90000) $140,001 - $200,000 = 28750 + 0.29 x (income – 140000) Greater than $200,000 = 46150 + 0.33 x (income – 200000) Dollar amounts should be in dollars and cents (float point numbers with two decimals shown). Tax is displayed on the screen.

1070


How to establish connection with oracle database software from c language?

1681