C 指针不是';t点
我有这个密码:C 指针不是';t点,c,pointers,struct,C,Pointers,Struct,我有这个密码: #include <stdio.h> #include <stdlib.h> #include <string.h> // Library stuff typedef struct { int x, y; } _TYPE_position; typedef struct { char image[32]; _TYPE_position position; } _TYPE_object; _TYPE_object
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Library stuff
typedef struct {
int x, y;
} _TYPE_position;
typedef struct {
char image[32];
_TYPE_position position;
} _TYPE_object;
_TYPE_object *object;
int createObject(const _TYPE_object *insertionObject);
int createObject(const _TYPE_object *insertionObject) {
int index;
_TYPE_object *newObjectPtr = realloc(object, sizeof(*object) + sizeof(_TYPE_object));
if(newObjectPtr != NULL) {
object = newObjectPtr;
index = sizeof(*object) / sizeof(_TYPE_object) - 1;
strcpy(object[index].image, insertionObject->image);
object[index].position = insertionObject->position;
}
else {
index =- 1;
}
return index;
}
// Demo program
int main(void) {
_TYPE_object smile = {
"smile.png", { 112, 80 }
};
int smileIndex = createObject(&smile);
if(smileIndex == -1) {
printf("Error allocating memory for smile object");
while(1);
}
smile.position.x = 55; // Does nothing since the object does not point here for some reason
int i;
for(i = 0; i < sizeof(*object) / sizeof(_TYPE_object); i++) {
printf("Create %s at { %d, %d }\n", object[i].image, object[i].position.x, object[i].position.y);
}
return 0;
}
#包括
#包括
#包括
//图书馆资料
类型定义结构{
int x,y;
}_类型_位置;
类型定义结构{
字符图像[32];
_类型_位置;
}_类型_对象;
_类型_object*object;
int createObject(常量类型对象*插入对象);
int createObject(常量类型对象*插入对象){
整数指数;
_TYPE_object*newObjectPtr=realloc(object,sizeof(*object)+sizeof(_TYPE_object));
if(newObjectPtr!=NULL){
object=newObjectPtr;
索引=sizeof(*对象)/sizeof(_类型_对象)-1;
strcpy(对象[索引]。图像,插入对象->图像);
对象[索引]。位置=插入对象->位置;
}
否则{
指数=-1;
}
收益指数;
}
//演示程序
内部主(空){
_类型\对象微笑={
“smile.png”{112,80}
};
int smileIndex=createObject(&smile);
如果(smileIndex==-1){
printf(“为微笑对象分配内存时出错”);
而(1),;
}
smile.position.x=55;//由于某种原因,对象没有指向此处,因此不执行任何操作
int i;
对于(i=0;i
这是一种工作方式。似乎该对象并没有指向_TYPE_对象,而是创建了它的精确副本
有没有办法使对象数组指向对象,而不仅仅是重新创建对象。所以在上面的例子中,有smile.position.x=55;实际更改打印的值。忽略sizeof()
问题,这里的问题是,通过执行以下操作,您正在显式复制给定给createObject()
的\u TYPE\u Object
字段,包括位置:
object[index].position = insertionObject->position;
因此,当您更改原件时(通过smile.position.x=55
),没有理由修改副本中的位置(即您正在打印的位置)
如果要插入对原始对象的引用而不创建副本,则必须创建指针数组,而不是对象本身的数组
index = sizeof(*object) / sizeof(_TYPE_object) - 1;
我相信你是想告诉你已经分配了多少个对象,但是你错了*object
并不是指整个数组,而是指数组中的第一个项object[0]
。它也根本不遵从或计算sizeof
中的表达式,因此您的代码相当于:
index = sizeof(_TYPE_object) / sizeof(_TYPE_object) - 1;
我建议您使用类似以下内容:
int objects_allocated = 0;
int objects_used = 0;
_TYPE_object *object = NULL;
您还使用了许多不必要的sizeof
表达式。您需要sizeof
来确定分配的大小,但是当您索引到数组中时,您不需要它。换言之:
&(object[n]) == ((void *)object) + n*sizeof(_TYPE_object)
您也不需要显式地复制每个成员。您可以使用:
object[index] = *insertionObject;
index=sizeof(*object)/sizeof(\u TYPE\u object)-1代码>索引将始终为零(因为sizeof/sizeof将始终为一)。使用\u type
作为类型名称的前缀是很麻烦的,并且违反了标准,在标准中,以\u
开头的符号后跟大写字母是为实现保留的。请记住:sizeof是一个运算符,在编译时应用。其结果实际上是一个常数。语法并没有说:sizeof object
,但是sizeof expression
sizeof()
并不像您想象的那样。对于类型t
上的指针p
,sizeof(*p)
始终返回1个元素t
的大小(以字节为单位),这与sizeof(t)
@wildplasser的大小相同:不一定是常量;c99允许可变大小的数组。但这实际上永远不会返回动态分配数组的大小。声明对象需要什么类型?int*object[]?这很有帮助。但我不是想把每个成员都复制过来。我希望对象数组引用\u TYPE\u对象变量。然后您希望\u TYPE\u object**object
,object=realloc(object,sizeof(\u TYPE\u object*)*num\u objects
,以及object[index]=insertionObject