这是什么,而(* P2 ++ = * P1 ++);意思? [英] What does while (*p2++ = *p1++); mean?

查看:571
本文介绍了这是什么,而(* P2 ++ = * P1 ++);意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个code座:

int main ()
{
    char *p1 = "Hello";
    char *p2;
    p2 = (char*)malloc (20);
    memset (p2, 0, 20);
    while (*p2++ = *p1++);
    printf ("%s\n", p2);
}

但我无法解释该行的工作
而(* P2 ++ = * P1 ++);
你可以让我知道这个操作公式中的顺序?

But I can not explain the working of the line while (*p2++ = *p1++); Could you let me know the order of operation in this formula?

推荐答案

这是经典的C code试图把一切在一行看起来非常聪明。

It's classic C code trying to look extremely clever by putting everything in one line.

而(* P2 ++ = * P1 ++); 等同于

strcpy(p2, p1);
p1 += strlen(p1) + 1;
p2 += strlen(p2) + 1;

在换句话说,它拷贝一个空值终止字符串, P1 结束了指向源字符串的结束和 P2 指向目标字符串的结尾。

In other words, it copies a null-terminated string, with p1 ending up pointing to the end of the source string and p2 pointing to the end of the destination string.

这篇关于这是什么,而(* P2 ++ = * P1 ++);意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆