我正在使用 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]; <----------
}
for( i = 0; i < size; ++i )
(table->array[i][i]).min = (table->array[i][i]).max = arr[i]; <----------
为什么它显示运行时错误?