昨天我正在面试一个中级软件工程职位的人,他提到在C中,NULL并不总是零,并且他已经看到了C的实现,其中NULL不为零.我觉得这很可疑,但我想确定.谁知道他是对的?
(回复不会影响我对这位候选人的判断,我已将决定提交给我的经理.)
我知道return和exit()(链接)之间的区别,但我不知道在何时何地选择一个而不是另一个.例如,根据这个答案,我理解这return是一个更好的选择,但从另一个我理解相反.
一个例子:在这个代码中(来自这个问题)是否优先使用exit()或return?
int read_file (char *filename, int **vet)
{
FILE *fin;
if ( !(fin = fopen(filename, "r")) )
{
perror(filename);
return -1;
}
* vet = malloc (10 * sizeof(int));
if ( *vet == NULL )
{
perror("Memory allocation error.\n");
return -2;
}
/* ... */
return fclose(fin);
}
int main ()
{
char filename[100];
int *vet;
if ( read_file(filename, &vet) ) …Run Code Online (Sandbox Code Playgroud)