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

Distinguish between actual and formal arguments.

592


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

603


what is the c source code for the below output? 5555555555 4444 4444 333 333 22 22 1 1 22 22 333 333 4444 4444 5555555555

2538


What are bitwise shift operators in c programming?

649


program to find out date after adding 31 days to a date in the month of febraury also consider the leap year

2579






can any one provide me the notes of data structure for ignou cs-62 paper

1707


Explain which of the following operators is incorrect and why? ( >=, <=, <>, ==)

611


What are c identifiers?

630


Multiply an Integer Number by 2 Without Using Multiplication Operator

324


write a program for the normal snake games find in most of the mobiles.

1787


Are there any problems with performing mathematical operations on different variable types?

576


Can you define which header file to include at compile time?

590


How do I round numbers?

602


How would you use the functions fseek(), freed(), fwrite() and ftell()?

707


Why static is used in c?

624