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

Answer Posted / lp

Declare a integer array size at runtime:
1.declare an interget pointer. - int *ptr
2.Get the size of array from user - int n
3.using malloc() ,allocate that many number of location to
the pointer. - ptr = malloc(n*sizeof(int));
4.Make use of the dynamically allocated array. - ptr[]

Is This Answer Correct ?    5 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

why programs in c are running with out #include? some warnings are display in terminal but we execute the program we get answer why? eg: main() { printf("hello world "); }

1324


The % symbol has a special use in a printf statement. Explain how would you place this character as part of the output on the screen?

672


What is 1f in c?

1849


Which is better between malloc and calloc?

678


What is difference between main and void main?

637






i have to apply for rbi before that i need to know the the syllabus for the entrance questions. whethet it may be aps or techinical

1849


Explain what is the difference between #include and #include 'file' ?

592


What is string length in c?

620


hello freinds next week my interview in reliance,nybody has an idea about it intervew questions..so tell

1678


program for reversing a selected line word by word when multiple lines are given without using strrev

1952


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

677


What is FIFO?

683


How can I find out how much free space is available on disk?

635


What is #include called?

578


What is the benefit of using const for declaring constants?

595