这是一个C程序,当我用gcc -Werror test.c进行编译时,gcc报错:
test.c: In function ‘InitList’:
test.c:20:59: error: expected expression before ‘tItemType’
tItemType *data = (tItemType *)malloc((sizeof tItemType) * LIST_MAX
代码如下:
#include <stdlib.h>
#include <stdbool.h>
#define LIST_MAX_LEN 129
typedef int tItemType;
typedef struct List
{
tItemType* data;
unsigned int length;
} tList;
bool InitList(tList *l)
{
if (l == NULL)
{
return false;
}
else
{
tItemType *data = (tItemType *)malloc((sizeof tItemType) * LIST_MAX_LEN);
if (data == NULL)
{
return false;
}
else
{
l->data = data;
l->length = 0;
}
}
}
int main(int argc, char *argv[])
{
tList l;
return 0;
}
但是,当我把malloc那条语句改成
tItemType *data = (tItemType *)malloc((sizeof(tItemType)) * LIST_MAX_LEN);
时,也就是将tItemType用括号抱起来之后,gcc没有再报错。这是为什么呢?
sizeof是C语言的一个关键字,sizeof tItemType和sizeof(tItemType)应该是没有区别的。这到底是怎么回事呢?