ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
How can I set an array's size at run time?
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: How can I set an array's size at run time?
Answer
# 1
You can't do that in C.  Use dynamic allocation method
to implement array.  See Q. 20 and 41 in 
http://www.geocities.com/vijoeyz/faq/

Best,
Vijay Zanvar,
Home Page - http://geocities.com/vijoeyz/

 
Is This Answer Correct ?    1 Yes 0 No
Vijay Zanvar
 
  Re: How can I set an array's size at run time?
Answer
# 2
by using calloc() or malloc() functions we create at run 
time 


        i think it will be correct if not forgive me 

              sumankumar..........
 
Is This Answer Correct ?    0 Yes 0 No
Suman
 
 
 
  Re: How can I set an array's size at run time?
Answer
# 3
This can be done by using malloc...if u have an integer 
array then ...you can ask user the size..and then allocate 
memory for integers...at the run time...using malloc()
 
Is This Answer Correct ?    0 Yes 0 No
Shalabh
 
  Re: How can I set an array's size at run time?
Answer
# 4
that is the main drawback of an array..
u cannot assign memory at run time...
 
Is This Answer Correct ?    0 Yes 2 No
Shruti
 
  Re: How can I set an array's size at run time?
Answer
# 5
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
        #include <errno.h>
        #include <stdio.h>

        #define     ROW     5
        #define     COL     5

        int
        main ( void )
        {
            int **cont_arr;
            int **cont_arr;

            int **arr = malloc ( ROW * sizeof ( int * ) );
            if ( !arr )
            {
                perror ( "Error" );
                exit ( EXIT_FAILURE );
            }

            for ( i=0; i < ROW; i++ )
            {
                arr[i] = malloc ( sizeof ( int ) * COL );
                if ( !arr[i] )
                {
                    perror ( "Error" );
                    exit ( EXIT_FAILURE );
                }
            }

            /*
             * Contiguous memory allocated for array.  
Below.
             */

            cont_arr = (int **) malloc ( ROW * sizeof ( int 
* ) );
            if ( !cont_arr )
            {
                perror ( "Error" );
                exit ( EXIT_FAILURE );
            }

            cont_arr[0] = (int *) malloc ( ROW * COL * 
sizeof ( int ) );
            if ( !cont_arr[0] )
            {
                perror ( "Error" );
                exit ( EXIT_FAILURE );
            }

            for ( int i=1; i < ROW; i++ )
                cont_arr[i] = cont_arr[0] + i * COL;

            exit ( EXIT_SUCCESS );
        }
 
Is This Answer Correct ?    1 Yes 1 No
Rajendra
 
  Re: How can I set an array's size at run time?
Answer
# 6
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 ?    0 Yes 1 No
Lp
 
  Re: How can I set an array's size at run time?
Answer
# 7
main()
{
 int n,i;
 scanf("%d",&n);
 int a[n];
}
 
Is This Answer Correct ?    2 Yes 6 No
Arthy
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
which of the following go out of the loopo if expn 2 becoming false a.while(expn 1){...if(expn 2)continue;} b.while(!expn 1){if(expn 2)continue;...} c.do{..if(expn 1)continue;..}while(expn 2); d.while(!expn 2){if(expn 1)continue;..} TCS2
int i =10 main() { int i =20,n; for(n=0;n<=i;) { int i=10 i++; } printf("%d", i); HCL5
why TCS selected more student in the software field from all institution. TCS3
how many argument we can pas in in a function CTS20
in C-programming language without using printf statement can we get output r not ? if yes how and if no also how ?  6
Write a program to accept a character & display its corrosponding ASCII value & vice versa?  4
which operator having lowest precedence?? a.)+ b.)++ c.)= d.)%  4
Can u return two values using return keyword? If yes, how? If no, why?  7
hi how to convert program from notepad to turboc editor can u please help me  1
How to find the given no is odd or even without checking of any condition and loops. (Hint: Using array)  4
proc() { static i=10; printf("%d",i); } If this proc() is called second time, what is the output? Hughes5
class foo { public: static int func(const char*& p) const; }; This is illegal, why? Google6
What is a class?  2
Reverse the part of the number which is present from position i to j. Print the new number. eg: num=789876 i=2 j=5 778986  1
hello friends what do u mean by BUS ERROR i got this error while i am doing my program in DATA STRUCTURES Wipro2
write a program to print the all 4digits numbers & whose squares must me even numbers? Virtusa2
what is array? HCL22
main() { int arr[5]={23,67}; printf("%d%d%d",arr[2],arr[3],arr[4]); } TCS6
Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it?  3
What are advantages and disadvantages of recursive calling ? HP11
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com