我有一个指向结构的指针,我想通过试验和错误获取其所有成员。我试图通过将指针递增1并去除它来运行结构。它应该从结构中返回一个正确的值(每i*sizeof(int)
时间),但它不会。运行结构并打印所有值?
我在做什么错?
fn (mach_msg_header_t *InHeadP, mach_msg_header_t *OutHeadP)
{
/*
assume that all struct members are int types
typedef struct
{
mach_msg_bits_t msgh_bits;
mach_msg_size_t msgh_size;
mach_port_t msgh_remote_port;
mach_port_t msgh_local_port;
mach_msg_size_t msgh_reserved;
mach_msg_id_t msgh_id;
} mach_msg_header_t;
size of the struct is 24.
*/
printf("ID: %d \n",InHeadP->msgh_id); //prints 'ID: 1337'
printf("Ptr: %p\n",InHeadP);
for (int i = 0; i <= 24; i++)
{
int deref = *((int*)(InHeadP+i));
printf("InHeadP[%d]=%d\n",i,deref);
//no sign of 1337 anywhere
}
}
P.S.我知道我不应该那样做,但这只是为了测试目的。
'INT * deref'?你不是指'int deref'吗? – nmichaels 2011-05-20 15:41:19
是的。 'int deref = *((int *)(InHeadP + i))'。改变了它。 – 2011-05-20 15:41:45
“它应该从结构中返回一个正确的值(每个I * sizeof(int)时间),但它不会。” - 呃为什么要这样?这不完全是如何分配内存。 – zellio 2011-05-20 15:42:32