<分区>
由于各种限制,我有一个方法只接受 int
争论。但是,我想将结构传递给此方法。
到目前为止,我已经...
main(int argc, char* argv){
Somestructure * name;
//Name is malloced, things are put in it, etc.
int address = (int) &name;
method(address);
}
void method(int arg){
Somestructure* thisStruct = (Somestructure*) arg;
//Do stuff with thisStruct.
}
我认为这会分配 thisStruct
指向与 main 方法中的名称相同的结构,但是当我尝试使用 thisStruct
时在method
,我得到一个总线错误。
当我使用这段代码时...
int structAddress = (int) &thisStruct;
printf("[Method] Address : %d\n", structAddress);
好像地址是name
(在 main
内)和 thisStruct
的(在 method
内)不同。
我对 C 有点陌生,但是,有人知道这里发生了什么吗?
此代码只能在 32 位系统上运行,因此我不必担心任何 64 位 int/地址问题。