c分配不兼容的类型,指针问题?
嗨,我正在使用C,我有一个关于分配指针的问题。
struct foo { int _bar; char * _car[SOME_NUMBER]; // this is meant to be an array of char * so that it can hold pointers to names of cars } int foofunc (void * arg) { int bar; char * car[SOME_NUMBER]; struct foo * thing = (struct foo *) arg; bar = thing->_bar; // this works fine car = thing->_car; // this gives compiler errors of incompatible types in assignment }
car和_car有相同的声明,为什么我收到有关不兼容类型的错误? 我的猜测是它与指针有关(因为它们是指向char *数组的指针,对吧?)但我不明白为什么这是一个问题。
当我宣布char * car;
而不是char * car[MAXINT];
它编译得很好。 但是当我需要使用索引访问某些信息时,我不知道这对我有什么用处,稍后访问该信息会非常烦人。 事实上,我甚至不确定我是否采用正确的方式,也许有更好的方法来存储一堆字符串而不是使用char *数组?
编辑:我不是故意使用INT_MAX(int的最大值),它只是一些其他的int,大约是20。
您正在创建一个大小为MAXINT的新数组。 我想你想创建一个指向MAXINT大小的数组的指针。
创建一个指向char *数组的指针:
以下是MAXINT到char *元素的数组:
char * car[MAXINT];
以下是指向以下内容的指针:大小为MAXINT到char *元素的数组:
char* (*car)[MAXINT];
以下是如何设置指针:一个大小为MAXINT到char *元素的数组:
char* (*car)[MAXINT]; car = &arg->_car;
问题中的其他语法错误:
- 在结构定义之后需要有分号。
- 你应该使用
foo*
而不是foo
。 所以它应该是:
struct foo* thing = (struct foo *) arg;
- 你应该使用的
thing
不是arg
:
bar = thing->_bar;
car = thing->_car;
car
和_car
都是数组,并且您不能在C中分配数组(除非数组嵌入在结构(或联合)中并且您执行结构分配)。
它们也是char的指针数组,而不是指向char数组的指针。 您在代码中编写的内容可能就是您想要的 – 您可以存储指向数组中MAXINT名称的指针。 但是,您应该正确描述类型 – 作为char或char指针的指针数组。
指向字符数组的指针如下所示:
char (*car)[MAXINT];
并指向一系列字符指针(谢谢,Brian)看起来像:
char *(*car)[MAXINT];
注意MAXINT; 这可能是一个非常大的数组(在Linux上,
将MAXINT
定义为INT_MAX
,至少为2 31 -1)。
代码如下:
struct foo { int _bar; char * _car[MAXINT]; } int foofunc (void * arg) { int bar; char * car[MAXINT]; struct foo thing = (struct foo *) arg; bar = arg->_bar; // this works fine car = arg->_car; // this gives compiler errors of incompatible types in assignment }
对于酒吧和汽车的任务都不应该编译 – arg
是void *
。 你可能想要使用某种形状或forms的thing
。 正如Brian所说,那里也存在问题:
你要么:
int foofunc(void *arg) { int bar; char *car[MAXINT]; struct foo thing = *(struct foo *)arg; bar = thing._bar; // this works fine car = thing._car; // this is still an array assignment ...other code using bar and car... }
或者你想要:
int foofunc(void *arg) { int bar; char *car[MAXINT]; struct foo *thing = (struct foo *) arg; bar = thing->_bar; // this works fine car = thing->_car; // this is still an array assignment ...other code using bar and car... }
或者,确实:
int foofunc(void *arg) { struct foo *thing = (struct foo *) arg; int bar = thing->_bar; // this works fine char *car[MAXINT] = thing->_car; // this is still an array assignment ...other code using bar and car... }
最后,处理数组赋值,在C中你可以合理地使用memmove()
来做到这一点:
int foofunc(void *arg) { struct foo *thing = (struct foo *) arg; int bar = thing->_bar; // this works fine char *car[MAXINT]; memmove(car, thing->_car, sizeof(car)); ...other code using bar and car... }
如果要复制的区域重叠,则类似的函数memcpy()
没有可靠的语义,而memmove()
则有; 总是使用memmove()
更简单,因为它总能正常工作。 在C ++中,您需要谨慎使用memmove()
(或memcpy()
)。 在这段代码中,它足够安全,但理解为什么是非平凡的。
你需要知道你只是在这里复制指针 – 你没有复制指针指向的字符串。 如果其他东西改变了这些字符串,它会影响通过car
看到的值和调用代码中的变量。
最后一点 – 现在:你确定你需要将函数的参数作为void *
吗? 它打开了各种滥用的代码,如果声明函数采用’ struct foo *
‘(甚至是’ const struct foo *
‘),可以防止这种滥用。
您无法按照自己的方式分配数组。 你可以做一个元素复制。
for(int i = 0; i < MAXINT; i++) { car[i] = (arg->_car)[i] }
请注意,如果字符串不是常量,则可能需要使用strcpy
。
C中的数组符号合法地令人困惑; 你的代码并不意味着你认为它意味着什么。
arg->_car
表示“数组_car
的地址”。 同样, car
意味着“arrayscar
的地址”。 如果你试图将_car的内容复制到汽车,那么这样做:
memcpy(car, _car, MAXINT);
但我认为,你真正的问题是“存储字符串列表的最佳方式是什么?” 这个答案是:动态列表(在您添加项目时自动增长的列表)。
你这样声明:
#define CARSIZE 65 int numCars = 0; char **car; /* a list of addresses, each one will point to a string */
添加汽车:
char *newCar = malloc(CARSIZE); /* make room */ strncpy(newCar, "Mercedes", CARSIZE); /* newCar has the address of the string */ car[numCars++] = newCar; /* store it */
列出汽车:
int n; for (n = 0; n < numCars; ++n) printf("%s\n", car[n]);
要移除位置n处的汽车:
free(car[n]); /* release the memory */ /* condense the list of pointers */ for ( ; n < numCars - 1; ++n) car[n] = car[n+1];
这完全是C中的例行程序。 注意:上面是我的头脑而不是从工作程序中复制,所以我不能保证所有的*都在正确的位置。 我怀疑这是一个家庭作业,所以我不想给你一切 ......