熊小丹小熊 发表于 2015-2-12 01:14:39

新人求助!关于指针的问题,谢谢各位!

#include<stdio.h>

void main()
{
        char *a = "My name is Dan!";
        char b[] = "Your name is Xinhao";
        void copy_string(char *from, char to[]);

        copy_string(a,b);
        printf("The a string is %s\nthe b string is %s\n", a, b);
}

void copy_string(char *from, char *to)
{
        to = from;

}

各位大侠,我想问的是为什么单步调试的时候,把a,b作为实参传递的,调试到copy_string函数里面显示的是 from指针已经复制给to指针了,然后继续单步调试,跳回main函数的时候,b仍然是原来的字符串呢,求解。
ps:正确的方法我已经从小甲鱼老师那里知道了,但是没有搞懂这个错误的原因,谢谢大家

百日维新 发表于 2015-2-13 10:23:55


void main()
{
    char *a = "My name is Dan!";
    char b[] = "Your name is Xinhao";
    void copy_string(char *from, char to[]);
   
   // copy_string(a,b);


   a = b;

   //b = a; óï·¨óD′í£¬bêÇò»¸öμØÖ·±eÃû2»¿éòÔÕaÑù¸3Öμ

    printf("The a string is %s\nthe b string is %s\n", a, b);
}

void copy_string(char *a, char *b)
{
    a = b;
   
}

百日维新 发表于 2015-2-13 10:27:28

函数调用之前会将a,b的保持起来(入栈),调用结束a,b的值还原(出栈)

熊小丹小熊 发表于 2015-2-14 03:46:13

百日维新 发表于 2015-2-13 10:27
函数调用之前会将a,b的保持起来(入栈),调用结束a,b的值还原(出栈)

谢谢!!简单明了好理解!
页: [1]
查看完整版本: 新人求助!关于指针的问题,谢谢各位!