C “怎么做?”;而(*s+;+;=*t+;+;)”;复制字符串?
我的问题是,这个代码是做什么的(来自): 网站上说上面的代码复制了一个字符串,但我不明白为什么C “怎么做?”;而(*s+;+;=*t+;+;)”;复制字符串?,c,C,我的问题是,这个代码是做什么的(来自): 网站上说上面的代码复制了一个字符串,但我不明白为什么 它与指针有关吗?是的,它与指针有关 读取代码的方法如下:“指针“s”指向的值(在该操作后递增)获取指针“t”指向的值”(该值在此操作后递增;此操作的整个值计算为复制的字符的值;在此操作中迭代,直到该值等于零)。因为字符串null终止符的值是零(“/0”)的字符值,循环将迭代,直到字符串从t指向的位置复制到s指向的位置。它复制字符串,因为数组总是通过引用传递,而字符串只是一个字符数组。基本上发生的是(如
它与指针有关吗?是的,它与指针有关
读取代码的方法如下:“指针“s”指向的值(在该操作后递增)获取指针“t”指向的值”(该值在此操作后递增;此操作的整个值计算为复制的字符的值;在此操作中迭代,直到该值等于零)。因为字符串null终止符的值是零(“/0”)的字符值,循环将迭代,直到字符串从t指向的位置复制到s指向的位置。它复制字符串,因为数组总是通过引用传递,而字符串只是一个字符数组。基本上发生的是(如果我记得正确的话)指针算术
您正在存储从s中的t取消引用的值,然后通过++移动到下一个索引。它通过将字符从“
t
”指向的字符串复制到“s
”指向的字符串中来工作。对于每个字符副本,两个指针都递增。循环在找到NUL时终止e> 字符(等于零,因此退出)。它等效于:
while (*t) {
*s = *t;
s++;
t++;
}
*s = *t;
当t
指向的字符是'\0'
时,while循环将终止。在此之前,它将复制t
指向s
指向的字符的字符,然后递增s
和t
以指向其数组中的下一个字符。是,这使用指针,还使用d计算while条件时执行所有工作。C允许条件表达式产生副作用
“*”运算符反引用指针s和t
递增运算符(“++”)在赋值后递增指针s和t
循环在空字符的条件下终止,该条件在C中计算为false
另一个注释…这不是安全代码,因为它无法确保s有足够的内存分配。提示:
- 运算符“=”的作用是什么
- 表达式“a=b”的值是多少?例如:如果你做“c=a=b”,c得到什么值
- 什么终止了C字符串?它的计算结果是真还是假
- 在“*s++”中,哪个运算符具有更高的优先级
忠告:
- 改为使用strncpy()
假设你有这样的东西:
char *someString = "Hello, World!";
someString指向字符串中的第一个字符—在本例中为“H”
现在,如果将指针增加1:
someString++
someString现在将指向“e”
while ( *someString++ );
将循环,直到someString指向的内容变为NULL,这表示字符串结束(“NULL终止”)
以及守则:
while (*s++ = *t++);
等于:
while ( *t != NULL ) { // While whatever t points to isn't NULL
*s = *t; // copy whatever t points to into s
s++;
t++;
}
这件事在幕后进行得太多了:
while (*s++ = *t++);
s
和t
变量是指针(几乎肯定是字符),s
是目标。以下步骤说明了发生的情况:
- t(
*t
)的内容被复制到s(*s
)中,一个字符
s
和t
都是递增的(++
)
- 赋值(复制)返回被复制的字符(在
期间被复制到)
while
将继续,直到该字符为零(在C
中字符串的末尾)
实际上,它是:
while (*t != 0) {
*s = *t;
s++;
t++;
}
*s = *t;
s++;
t++;
但是写出来的方式要简洁得多。关于这一点的神秘之处在于操作顺序。如果你查阅C语言规范,它说明在这种情况下,操作顺序如下:
1. * operator
2. = (assignment) operator
3. ++ operator
因此,while循环在英语中变成:
while (some condition):
Take what is at address "t" and copy it over to location at address "s".
Increment "s" by one address location.
Increment "t" by one address location.
而(某些条件):
将地址“t”处的内容复制到地址“s”处的位置。
按一个地址位置递增“s”。
将“t”增加一个地址位置。
现在,什么是“某些条件”?C语言规范还说赋值表达式的值是赋值本身,在本例中是*t
所以“某些条件”是“t
指向非零的对象”,或者用更简单的方式,“而位置t
处的数据不是NULL
”启动while循环
*首先是s=*t,它将t指向的内容分配给s指向的内容。也就是说,它将一个字符从t字符串复制到s字符串
所分配的内容被传递给while条件…任何非零都是“true”,因此它将继续,而0是false,它将停止…并且恰好字符串的结尾也是零
s++和t++它们增加指针
一切又开始了
因此,它不断分配循环,移动指针,直到它碰到一个0,它是字符串的结尾。让我们假设s
和t
是指向字符串的char*
s(并且假设s
至少与t
一样大)。在C中,字符串都以0
结尾(ASCII“NUL”),对吗?那么这是做什么的:
*s++ = *t++;
首先,它执行*s=*t
,将*t
处的值复制到*s
。然后执行s++
,因此s
现在指向下一个字符。然后执行t++
,因此t
指向下一个字符。这与运算符优先级和前缀与后缀增量/减量有关
运算符优先级是运算符解析的顺序。有关简单示例,请参见:
4 + 2 * 3
这是4+(2*3)
还是(4+2)*3
?好吧,我们知道它是第一个,因为它具有优先级-二进制*
(乘法运算符)具有更高的优先级
4 + 2 * 3
while(*s++ = *t++);
char *s, *string = s;
while(*s++ = *t++);
printf("%s", string); // prints the string that was in *t
size_t i = strlen(t);
while(*s++ = *t++);
s -= i + 1;
printf("%s\n", s); // prints the string that was in *t
size_t i = strlen(t);
char *c = malloc(i + 1);
while(*s++ = *t++);
s -= i + 1;
printf("%s\n", s); // prints the string that was in *t
free(c);
"I am a string"
printf("hello, world\n");
char *pmessage;
pmessage = "now is the time";
char amessage[] = "now is the time"; /* an array */
char *pmessage = "now is the time"; /* a pointer */
+---+ +--------------------+
pmessage: | o-------->| now is the time \0 |
+---+ +--------------------+
+--------------------+
amessage: | now is the time \0 |
+--------------------+
/* strcpy: copy t to s; array subscript version */
void strcpy(char *s, char *t)
{
int i;
i = 0;
while((s[i] = t[i]) != '\0')
i ++;
}
/* strcpy: copy t to s; pointer version 1 */
void strcpy(char *s, char *t)
{
while((*s = *t) != '\0')
{
s ++;
t ++;
}
}
/* strcpy: copy t to s; pointer version 2 */
void strcpy(char *s, char *t)
{
while((*s++ = *t++) != '\0')
;
}
/* strcpy: cope t to s; pointer version 3 */
void strcpy(char *s, char *t)
{
while(*s++ = *t++);
}
while(*s++ = *t++);
while (*t) {
*s = *t;
s++;
t++;
}
*s = *t; /* if *t was 0 at the beginning s and t are not incremented */
char tmp = 0;
do {
tmp = *t;
*s = tmp;
s++;
t++;
} while(tmp);
for (;;) {
char *olds = s; // original s in olds
char *oldt = t; // original t in oldt
char c = *oldt; // original *t in c
s += 1; // complete post increment of s
t += 1; // complete post increment of t
*olds = c; // copy character c into *olds
if (c) continue; // continue if c is not 0
break; // otherwise loop ends
}
char t[]="I am a programmer",s[20];
for(int i=0;*(t+i)!='\0';i++)
*(s+i)=*(t+i);
*(s+i)=*(t+i); //the last char in t '\0'
printf("t is:%s\n",t);
printf("s is:%s\n",s);
char t[]="I am a programmer",s[20];
char *p1,*p2;
p1=t,p2=s;
for(;*p1!='\0';p1++,p2++)
*p2 = *p1;
*p2 = *p1;
printf("t is:%s\n",t);
printf("s is:%s\n",s);
char t[]="I am a programmer",s[20];
char *p1,*p2;
p1=t,p2=s;
while(*p2++=*p1++);
printf("t is:%s\n",t);
printf("s is:%s\n",s);
printf("t is:%s\n",p1-18);
printf("s is:%s\n",p2-18);
char a[20],*t="I am a programmer",*s;
s=a;
while(*s++=*t++);
printf("t is:%s\n",t-18);
printf("s is:%s\n",s-18);
printf("s is:%s\n",a);