DPを使用して、配列内のインデックスiからインデックスjまでの最大項目と最小項目を含むテーブルを埋めています。このアプローチが効率的であるかどうかは、私にとって問題ではありません。
以下のコードスニペットについて考えてみます。
typedef struct Slot
{
int min;
int max;
}Slot;
typedef struct Table
{
Slot* *array;
int size;
}Table;
Table* createTable( int size )
{
Table* table = malloc( sizeof( Table ) );
if( !table )
return NULL;
table->array = ( Slot(*)[size] ) malloc( size * size * sizeof( Slot ) );
if( !(table->array) )
return NULL;
table->size = size;
return table;
}
void foo( int arr[], int size )
{
Table* table = createTable( size );
if( table == NULL )
{
printf( "Out of memory" );
return;
}
int i;
for( i = 0; i < size; ++i )
(table->array[i][i]).min = (table->array[i][i]).max = arr[i]; <----------
}
ランタイムエラーが発生します。以下の2つのステートメントにコメントすると、正常に実行されます。
for( i = 0; i < size; ++i )
(table->array[i][i]).min = (table->array[i][i]).max = arr[i]; <----------
なぜランタイムエラーが表示されるのですか?