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

Write a code to remove duplicates in a string.

822


What is the use of #define preprocessor in c?

822


pgm to find any error in linklist(in single linklist check whether any node points any of previous nodes instead of next node)

2416


what is stack , heap ,code segment,and data segment

2436


Why is this loop always executing once?

803


Explain what is operator promotion?

831


What is methods in c?

832


What is the difference between procedural and declarative language?

890


Write a program to swap two numbers without using the third variable?

808


What is a program flowchart and explain how does it help in writing a program?

968


.find the output of the following program? char*myfunc(char*ptr) { ptr +=3; return (ptr); } int main() { char*x,*y; x="HELLO"; y=myfunc(x); printf("y = %s ",y); return 0; }

2306


What is difference between far and near pointers?

791


What is a double c?

773


What would happen to X in this expression: X += 15; (assuming the value of X is 5)

1659


Can we declare variable anywhere in c?

724