#include<stdio.h>
#include<stdlib.h>
int main()
{
char source[]="hello"; //创建一个字符串数组值为“hello”
char* des =(char*)malloc(*sizeof(char)); //初始化一个长度为5的空的字符串数组 for(int i=;i<;i++) //通过for循环将source中的元素拷贝到des中
{
*des++=*source++;
} printf("%s",des);
return ;
}

结果:

lvalue require as increment operand-LMLPHP

编译器报错:lvalue require as increment operand (错误在第10行)

自己的理解:

  原来 在这里如果要使用 *des++ 或者 *source++ 那么 des 或 source 就需要是个能进行加一操作的指针也就是地址,然而在上面的代码中

des 和 source 并不是个地址 而是两个字符串数组;

  那么按照这个想法,改变一下,先定义两个 指针 char* c 和 char* k 分别指向两个字符串数组的首地址,然后再对 这两个指针进行增加加操作

 #include<stdio.h>
#include<stdlib.h>
int main()
{
char source[]="hello";
char* des =(char*)malloc(*sizeof(char)); char* c = des;
char* k = source;
for(int i=;i<;i++)
{
*c++=*k++;
}
printf("%s",des);
return ;
}

结果:

编译成功无报错,并得到了预期的结果

lvalue require as increment operand-LMLPHP

补充:

字符串拷贝的典型实现:

 char *strcpy(char *des, char * source) //des 为目标字符串数组,source为源数组
{
char* r = des;
/*
assert 来自于c标准库<assert.h>,表示如果括号中的表达式为false则终止程序执行
为true不做任何操作
*/
assert((des != NULL)&&(source != NULL));
while((*r++ = *source++)!='\0');
return des;
}
05-18 17:44