将'char *[12]'类型赋值为'int'类型时的不兼容类型

时间:2021-08-06 16:12:20

I have an array of char*

我有一个char*数组

#define SEASRCH_ENGINES_QUEUE_MAX_SIZE  12
static char* searchEnginesNamesQueue[SEASRCH_ENGINES_QUEUE_MAX_SIZE];

I want to nullify it after I free its memory:

我想在我释放了它的记忆后将它取消:

searchEnginesNamesQueue = NULL;

searchEnginesNamesQueue =零;

but I get this error:

但是我得到了这个错误:

incompatible types when assigning to type 'char *[12]' from type 'int'

将'char *[12]'类型赋值为'int'类型时的不兼容类型

2 个解决方案

#1


4  

That is an array, and arrays cannot be assigned to.

这是一个数组,数组不能被赋值。

Also, you cannot "free its memory", arrays cannot be freed, you can only free dynamically allocated ("heap") memory.

此外,您不能“释放它的内存”,不能释放数组,只能释放动态分配(“堆”)的内存。

If you mean you have had heap memory pointers stored in the array, which have then been freed, you must set each pointer in the array to NULL:

如果您的意思是您有存储在数组中的堆内存指针,然后释放,那么您必须将数组中的每个指针设置为NULL:

for(int i = 0;
    i < sizeof searchEnginesNamesQueue / sizeof *searchEnginesNamesQueue;
    ++i)
{
  searchEnginesNamesQueue[i] = NULL;
}

The loop can easily be written without braces, but I had to put the header on multiple lines to avoid horizontal scrolling.

无需大括号就可以轻松编写循环,但我必须将标题放在多行上,以避免水平滚动。

Note nifty use of sizeof to have the compiler compute the loop boundary.

注意,使用sizeof让编译器计算循环边界。

#2


1  

Basically NULL is an integer and your searchEnginesNamesQueue is an array of character pointer. you just can't assign it like that. Try value of char pointer to NULL, maybe like this *char = NULL.

基本上,NULL是一个整数,而你的searchEnginesNamesQueue是一个字符指针数组。你不能这样分配。将char指针的值尝试为NULL,可能像这个*char = NULL。

#1


4  

That is an array, and arrays cannot be assigned to.

这是一个数组,数组不能被赋值。

Also, you cannot "free its memory", arrays cannot be freed, you can only free dynamically allocated ("heap") memory.

此外,您不能“释放它的内存”,不能释放数组,只能释放动态分配(“堆”)的内存。

If you mean you have had heap memory pointers stored in the array, which have then been freed, you must set each pointer in the array to NULL:

如果您的意思是您有存储在数组中的堆内存指针,然后释放,那么您必须将数组中的每个指针设置为NULL:

for(int i = 0;
    i < sizeof searchEnginesNamesQueue / sizeof *searchEnginesNamesQueue;
    ++i)
{
  searchEnginesNamesQueue[i] = NULL;
}

The loop can easily be written without braces, but I had to put the header on multiple lines to avoid horizontal scrolling.

无需大括号就可以轻松编写循环,但我必须将标题放在多行上,以避免水平滚动。

Note nifty use of sizeof to have the compiler compute the loop boundary.

注意,使用sizeof让编译器计算循环边界。

#2


1  

Basically NULL is an integer and your searchEnginesNamesQueue is an array of character pointer. you just can't assign it like that. Try value of char pointer to NULL, maybe like this *char = NULL.

基本上,NULL是一个整数,而你的searchEnginesNamesQueue是一个字符指针数组。你不能这样分配。将char指针的值尝试为NULL,可能像这个*char = NULL。