typedef struct _DocumentRow
{
char * code /** The code */;
char * designation /** The designation */;
double quantity /** The quantity */;
char * unity /** The unity */;
double basePrice /** The base price */;
double sellingPrice /** The selling price */;
double discount /** The discount */;
double rateOfVAT /** The rate of VAT */;
struct _DocumentRow * next /** The pointer to the next row */;
} DocumentRow;
void DocumentRowList_init(DocumentRow ** list) {
DocumentRow *L;
list = ( DocumentRow ** ) malloc( sizeof( DocumentRow* ) );
if ( list == NULL ) {
fatalError( "memory is not enough" );
}
L = NULL;
list = &L;
}
在使用函数 DocumentRowList_init
后,当我测试 if ( *list == NULL )
时,它的计算结果为 false,为什么?我已经设置了 list = &L
和 L = NULL
。
最佳答案
你会在这里有未定义的行为。 L
是局部变量,因此当您通过指针指针返回它的地址时,该变量在 DocumentRowList_init
返回时不再存在。
因此,即使您将 NULL
分配给它,它也会指向无效内存。
但是 list
是 DocumentRowList_init
的局部变量,因此它无论如何都不会返回值,因为您只是为其分配一个值然后返回。
如果你想返回 DocumentRow
的结构,你必须使用这个
*list = malloc( sizeof *L);
分配一个结构并返回指向它的指针。
关于c - 如何将指向结构的指针设置为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20899158/