晓觉懿 发表于 2020-7-6 22:27:34

*p++=*q++ 能用于拷贝字符吗?

如图为什么运行不正常?

Hello. 发表于 2020-7-6 22:33:27

可以,能否把代码贴过来哈

晓觉懿 发表于 2020-7-6 22:39:54

Hello. 发表于 2020-7-6 22:33
可以,能否把代码贴过来哈

#include<stdio.h>
int main(void)
{
        char str={'h','e','l','l','o'};
        char a;
        char *p,*q;
        p=str;       
        q=a;
        *p++=*q++;
       
        for(int i=0;i<6;i++)
        {
        printf("%c",a);
        }
       
}

小甲鱼的铁粉 发表于 2020-7-6 23:18:26

晓觉懿 发表于 2020-7-6 22:39
#include
int main(void)
{


#include<stdio.h>
int main(void)
{
      char str={'h','e','l','l','o'};
      char a;
      char *p,*q;
      p=str;      
      q=a;
      *q++=*p++;
      
      for(int i=0;i<6;i++)
      {
             printf("%c",a);
      }
      
}
楼主你q和p弄反了{:10_256:}

赚小钱 发表于 2020-7-7 01:19:26

本帖最后由 赚小钱 于 2020-7-7 01:26 编辑

没有 for / while 循环,只会执行一次。

赋值语句,是将 = 右侧的值,赋值给 = 左侧的变量。

chxchxkkk 发表于 2020-7-7 10:47:01

*q++=*p++; 这一句要放到个循环里
比如
while(*q != '\0')
{
    *q++=*p++;
}

热气球 发表于 2020-7-7 14:33:04

#include<stdio.h>
int main(void)
{
      char str={'h','e','l','l','o'};
      char a;
      char *p,*q;
      p=str;      
      q=a;
      for(int i=0;i<6;i++)
      {
            *q++=*p++;
             printf("%c",a);
      }
      
}
可参考此代码

405794672 发表于 2020-7-7 15:58:37

你将q的值,也就是a数组里面的值赋给了p,也就是str数组。而a数组并未初始化,里面是一堆内存废弃值,所以打出来是你不知道的东西

永恒的蓝色梦想 发表于 2020-7-10 14:12:37

小甲鱼的铁粉 发表于 2020-7-6 23:18
楼主你q和p弄反了

还有没有循环的问题。
页: [1]
查看完整版本: *p++=*q++ 能用于拷贝字符吗?