【发布时间】:2015-10-15 16:14:23
【问题描述】:
我有这个:
typedef struct{
int x;
int y;
}T;
void f(T** t)
{
T t1;
*t=malloc(sizeof(T)*T_MAX_SIZE);
t1.x=11;
t1.y=12;
(*t)[0] = t1;
}
我希望它可以移动指针,而不是使用位置,我不确定问题出在哪里或出什么问题,代码:
void f(T** t)
{
T t1;
T t2;
T** copy=t;
*t=malloc(sizeof(T)*T_MAX_SIZE);
t1.x=11;
t1.y=12;
t2.x=21;
t2.y=22;
**copy=t1;
copy++;
**copy=t2;
}
int main()
{
T* t;
f(&t);
printf("%i %i\n",t[0].x,t[1].x);
free(t);
}
这是以下线程的继续 ->Copying Struct to a Pointer array in a function C
这不起作用:/
【问题讨论】:
-
我强烈建议阅读一本好的 C 书籍中的指针。对我来说,它看起来像是原始问题的重复。如果不是,则不清楚您真正想要完成什么。指针指向内存中的“位置”。
标签: c pointers struct dynamic-memory-allocation