指针的地址、定义时连等赋值、指针的连等赋值
发布于 2006-02-06 12:13 2 阅读:23,751 评论:2 标签: 指针

  1:指针的地址可以由以下方法得到:

    int a = 5;
    int *pA = &a;
    cout<<"The &pA is :"<<&pA<<endl;

  2:在定义的时候赋值是不能连等赋值的:

  例如下面

    int x = y = z = 5;

  此时,编译器会对y 、z报错。同样,对于指针也是如此,例如:

    int *m = *n = *w = 0;

  此时,编译器会对n 、w报错。

  3:对于指针的连等赋值建议使用地址连等赋值:

  对于以下程序,编译器不会报错:

    int *m =0 , *n = 0 , *w = 0;
    *m = *n = *w = 8;//把8改为x(x已定义赋值)也一样

  但是运行程序的时候会出现错误(至少对偶的机子是这样)。Windows XP提示:

    “0x004013bd”指令引用的“0x00000000”内存。该内存不能为“written”。

  所以建议使用:

       m = n = w = &x;           

没注意到已经有地址了

  只看见书上说定义指针时要注意给它地址赋初值,一时糊涂想到在这里已经有地址值了。 

by 芽雨 2006-02-06 14:14:00
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
貌似对“*”运算符理解不清

使用*单目运算符,则是对这个地址指向的值进行操作。也就是说:
*m = *n = *w = 8;
这一句是将m、n、w指向的值改写为8,而这个位置是0(NULL),一个无效地址,对无效地址指向的值进行写入则会导致程序崩溃。

by 李马 2006-02-06 13:14:00
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172