C语言:为什么我得到NULL?
关于代码:
tp 是指向包含表的某个结构的指针。
该表是一个指向不同结构的指针,用作数组。
size 只是桌子的大小。
我将这些变量发送到一个函数,以便将
数组中的所有单元格初始化为 NULL。
这一行:
initArr(tp->table,tp->size);
将它们发送到此函数:
void initArr(ObjectP* array,int size)
{
int i;
for (i = 0; i < size; ++i)
{
array[i]=NULL;
}
}
使用 Eclipse 调试器,我可以看到数组中的对象实际上被
初始化为 NULL,但是当方法结束时,
tp->table 为 NULL。
指针变得疯狂?
请帮忙。
结构:
表:
typedef struct Table
{
size_t size;
hashFcn hash;
printFcn print;
comparisonFcn comp;
ObjectP* table;
int duplicated;
}Table;
对象:
typedef struct Object
{
void *key;
ObjectP pointsTo;
}Object;
about the code:
tp is a pointer to a certain struct which contains a table.
the table is a pointer to a pointer of a differnt struct,used as an array.
size is just the size of the table.
im sending these veriables to a function in order to initialize all the cells in the
array to NULL.
this line:
initArr(tp->table,tp->size);
sends them to this function:
void initArr(ObjectP* array,int size)
{
int i;
for (i = 0; i < size; ++i)
{
array[i]=NULL;
}
}
using the eclipse debugger i can see that the objects in the array are infact
being initialized to NULL, but when the method ends,
tp->table is NULL.
pointers gone wild?
help please.
the structs:
table:
typedef struct Table
{
size_t size;
hashFcn hash;
printFcn print;
comparisonFcn comp;
ObjectP* table;
int duplicated;
}Table;
object:
typedef struct Object
{
void *key;
ObjectP pointsTo;
}Object;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数组和指针相似但又不同。
指针数组可以表示为内存中的多个连续指针(具有数组中第一个指针所在的地址)。
在这种情况下,tp->table与tp->table[0]完全相同,但假定[0](因为它具有相同的地址)。在以这种方式实现的系统中,tp->table指定一个地址,并且从该地址的偏移量(到达数组的元素)被表示为一个值乘以数据类型大小(或一个指针的大小)你的情况)。
因此,您的调试器实际上可能会打印出 tp->table ,这与 tp->table[0] 完全相同,具体取决于编译器的实现。
Arrays and pointers are similar but different.
An array of pointers can be represented as a number of continuous pointers in memory (with an address of where the first pointer in the array resides).
Under such a circumstance tp->table is exactly the same as tp->table[0], but the [0] is assumed (because it has the same address). In systems that are implemented in this manner, the tp->table specifies an address, and the offset from that address (to get to the element of the array) is represented as a value times the datatype size (or one pointer's size in your case).
So your debugger might actually be printing out tp->table which is exactly equivalent to tp->table[0] depending on your compiler's implementation.
所提供的代码似乎是错误的(您正在从 void 函数返回某些内容!),但我将进行大胆的猜测并假设在您的实际代码中,您正在尝试将“数组”设置为某些内容(可能通过 malloc )在 initArr 中,在这种情况下,我们有一个经典的陷阱:您正在按值传递 tp->table,因此它不会被 initArr 更改:initArr 在本地副本上运行tp->table,当 initArr 结束时被丢弃:)
编辑:
Doh - 现在你已经发布了更新,看起来我的猜测是错误的。 :/想象一下,如果我做对了,那该多好啊! :)
The code as presented seems wrong (you are returning something from a void function!), but I'm going to take a wild guess and assume that in your actual code, you are trying to set "array" to something (probably via malloc) inside initArr, in which case we have a classic gotcha: You are passing tp->table by value, so it does not get changed by initArr: initArr operates on a local copy of tp->table, which is discarded when initArr ends :)
Edit:
Doh - now you've posted the update, it looks like my guess was wrong. :/ Imagine the kudos if I'd got it right! :)