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

Answers were Sorted based on User's Feedback



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

Answer / suman

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 ?    4 Yes 0 No

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

Answer / shalabh

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 ?    4 Yes 0 No

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

Answer / vijay zanvar

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 ?    3 Yes 0 No

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

Answer / 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

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

Answer / rajendra

#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 ?    4 Yes 2 No

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

Answer / vinod

C99 and above supports the conceptof VLA(Variable Length Array) which allows you to set array size based on input during run time. It can also done using dynamic memory allocation.

Example:
int main()
{
int i;
scanf("%d",&i);
int a[i];
}

Is This Answer Correct ?    1 Yes 0 No

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

Answer / 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

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

Answer / shruti

that is the main drawback of an array..
u cannot assign memory at run time...

Is This Answer Correct ?    2 Yes 5 No

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

Answer / arthy

main()
{
int n,i;
scanf("%d",&n);
int a[n];
}

Is This Answer Correct ?    4 Yes 25 No

Post New Answer

More C Interview Questions

What are keywords in c with examples?

0 Answers  


Write a simple program to find the size of different basic data types in C.

3 Answers  


how to add numbers without using arithmetic operators.

14 Answers   TCS,


How can I handle floating-point exceptions gracefully?

0 Answers  


The variables are int sum=10,SuM=20; these are same or different?

3 Answers  






Is linux written in c?

0 Answers  


What is wrong with this code?

0 Answers  


Explain the use of #pragma exit?

0 Answers  


Hello. How to write a C program to check and display president party like if i type in the console "biden" and hit enter the output shoud be : "biden is democrat" and if i type "trump" and hit enter the output shoud be: "trump is republican"

0 Answers  


Write a program to generate prime factors of a given integer?

9 Answers   Microsoft,


p*=(++q)++*--p when p=q=1 while(q<=6)

0 Answers   KINPOE,


What is operator precedence?

0 Answers  


Categories