【发布时间】:2018-02-16 10:37:17
【问题描述】:
我有一个简单的结构,loc
typedef struct
{
long blk;
int offset;
} loc;
在函数avl_isnadd中,它被传入为:
int
avl_isnadd (old_loc, old_isn, isn)
loc *old_loc;
int old_isn, isn;
{
int next_isn;
loc *this_loc;
printf("\n{avl_isnadd} - old_loc-> blk = %d, old_loc->offset = %d\n", old_loc->blk, old_loc->offset);
this_loc->blk = old_loc->blk;
this_loc->offset = old_loc->offset;
printf("\n{avl_isnadd} - this_loc->blk = %d, this_loc->offset = %d\n", this_loc->blk, this_loc->offset);
next_isn = avl_isnget (this_loc);
return next_isn;
}
在 avl_isnget 中,我们有:
int
avl_isnget (myLoc)
loc *myLoc;
{
printf("\n{avl_isnget} - MyLoc->blk = %d, myLoc->offset = %d\n", myLoc->blk, myLoc->offset);
return 0;
}
控制台上的结果是:
{avl_isnadd} - old_loc-> blk = 1, old_loc->offset = 512
{avl_isnadd} - this_loc->blk = 1, this_loc->offset = 512
{avl_isnget} - MyLoc->blk = 1485457792, myLoc->offset = 512
我在这里缺少什么?我不明白为什么 avl_isnget 应该有 myLoc->blk
的不同值【问题讨论】:
-
您没有为
this_loc分配任何空间。只要您执行this_loc->blk = old_loc->blk;,您就会调用未定义的行为。 -
那么,如果我将其定义为loc this_loc,我该如何做赋值并将其作为指针传递给函数呢?
-
你需要分配一些内存。买一本更新的 C 书——老式的参数列表在 30 年前就已经过时了。
-
自 28 年以来,这已过时且强烈反对!买一本最近的书,这也将有助于理解指针和内存分配!
-
如果将其定义为
loc this_loc,则使用&得到this_loc的指针,即:&this_loc。你在使用 C 文档吗?