scanf("%d %d", &a, &b);
double array1[a][b];
C99, ( , , , , , ..).
array1 - (VLA), C99. C2011, , . , __STDC_NO_VLA__ - , VLA.
size_t a, b;
if ( scanf( "%zu %zu", &a, &b ) < 2 )
#if defined( __STDC_VERSION__ ) && __STDC_VERSION__ >= 199901L && !defined( __STDC_NO_VLA__ )
if ( a > SOME_MAX_LIMIT || b > SOME_MAX_LIMIT )
double array1[a][b];
#else
double **array1 = malloc( sizeof *array1 * a );
if ( array1 )
{
size_t i;
for ( i = 0; i < a; i++ )
{
array1[i] = malloc( sizeof *array1[i] * b );
if ( !array1[i] )
break;
}
if ( i < a )
{
while ( i-- )
free( array1[i] );
free( array1 );
}
}
else
#endif
array1[i][j], , . :
#if !defined( __STDC_VERSION__ ) || __STDC_VERSION__ < 199901L || defined( __STDC_NO_VLA__ )
for ( size_t i = 0; i < a; i++ )
free( array1[i] );
free( array1 );
#endif
, malloc.
VLA , auto () - , .
, ( ), static, struct union.
VLA :
size_t a, b;
if ( scanf( "%zu %zu", &a, &b ) < 2 )
double (*array1)[b] = malloc( sizeof *array1 * a );
You get a dynamically distributed 2D array with user-defined dimensions that are contiguous, and you are limited only by the size of the heap, not the size of the stack, so you can select large objects this way.
source
share