[英]How to check for NULL in C of an array of integer pointers?
I have a fixed size array of integer pointers where I want to allocate from heap only if it is needed, but I can't seem to be able to get the following NULL check to work. 我有一个固定大小的整数指针数组,仅在需要时才想从堆中分配它,但是我似乎无法使以下NULL检查起作用。 malloc never get called first. 永远不会首先调用malloc。
int* seqList[n];
for (int i = 0; i < q; i++) {
int type, x, y;
scanf("%d", &type);
scanf("%d", &x);
scanf("%d", &y);
if (type == 1) {
if (seqList[x] != NULL) {
int size = sizeof(seqList[x])/sizeof(int*);
seqList[x] = realloc(seqList[x], (size + 1)*sizeof(int*));
seqList[x][size] = y;
}
else {
seqList[x] = malloc(sizeof(int*));
}
}
else{
...
}
}
You have to initialize it to 'NULL' to NULL check the array of pointers . 您必须将其初始化为'NULL',以NULL检查指针数组。
int* seqList[n] = {NULL};
or 要么
for ( i =0 ; i < n ; i++)
seqList[i] = NULL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.