为什么void指针会给出与它指向的地址不同的地址?

时间:2016-03-11 01:35:56

标签: c pointers void-pointers

#include <stdio.h>

int main(int argc, const char * argv[]) {
    int *pi = NULL;
    void *voidptr = pi;
    int num = 100;
    pi = &num;

    printf("%p\n",pi);
    printf("%p\n", voidptr);
    return 0;
}

为什么第二个printf会给出0x0但地址不同于第一个?

1 个答案:

答案 0 :(得分:2)

由于对一个对象的分配不会影响C中的另一个对象,因此voidptr pi的值为NULL,因此printf()尚未更新。分配给它。

顺便提一下,您通过将具有错误类型的数据传递给int*来调用未定义的行为:您通过%pvoid*期望public class Form1 { public Form1() { InitializeComponent(); } // SomeButton is Clicked public void SomeButton_Click(object sender, EventArgs e) { // SomeButton is disabled SomeButton.Enabled = false; // Form2 is created var form2 = new Form2(); // Subscribing to Form2's Closed event form2.Closed += OnClosed; } private void OnClosed(object sender, EventArgs eventArgs) { // Event is fired and you can enable the button SomeButton.Enabled = true; } }