简体   繁体   English

strcpy中的指针赋值如何工作?

[英]How does the pointer assignment in strcpy work?

I'm looking at a strcpy example where they increase the value of a pointer, and assign it in 1 line, like this: 我正在看一个strcpy示例,它们增加指针的值,并将其分配为1行,如下所示:

*ptrA++ = *ptrB++;

I know that the value where the pointer is pointing to in the char array is increased, and the contents is copied. 我知道指针在char数组中指向的值会增加,并且内容会被复制。

does c do something like 做c之类的事情

*ptrA = *ptrB;
ptrA++;
ptrB++;

in the background ? 在后台?

Yes it does, remember that the postfix ++ means return the value before the increment. 是的,记住postfix ++意味着在增量之前返回值。 So *ptrA++ increments ptrA but returns the dereference of of ptrA before the increment. 所以* ptrA ++增加ptrA但在增量之前返回ptrA的解引用。

Well, yes and no. 嗯,是的,不。

Yes, because the second piece of code you provided does indeed do the same thing as the original code. 是的,因为您提供的第二段代码确实与原始代码完全相同。 So, in a way, you understood your original code correctly. 因此,在某种程度上,您可以正确理解原始代码。

No, because your second piece code is not really equivalent to the original one. 不,因为你的第二段代码与原始代码并不完全相同。 Remember, it is incorrect to say that postfix ++ operator returns the original value first and increments the pointer later . 请记住,这是不正确的说,后缀++运算符将首先返回原值和后来增加的指针。 In C language temporal relationships (what happens "before" and what happens "after") can only be defined by sequence points . 在C语言中,时间关系(“之前”发生的事情和“之后发生的事情”)只能由序列点定义。 The expression 表达方式

*ptrA++ = *ptrB++;

has no sequence points inside, so there's absolutely no way to say what happens before and what happens after. 内部没有序列点,所以绝对没有办法说出之前发生的事情以及之后发生的事情。 At the same time, your second variant 与此同时,你的第二个变种

*ptrA = *ptrB;
ptrA++;
ptrB++;

explicitly guarantees that increment happens after the dereference, since there's a sequence point at the end of each statement. 显式保证在取消引用发生增量,因为在每个语句的末尾都有一个序列点。 There's no such guarantee with regard to the first variant. 对于第一个变体,没有这样的保证。 This is what I see as a problem with your interpretation. 这就是我认为你的解释存在的问题。

In reality it is quite possible that the increment will happen first and the dereference will happen later. 实际上,增量将很可能首先发生,并且取消引用将在稍后发生。 For example, the compiler can translate your original expression into something like 例如,编译器可以将原始表达式转换为类似的内容

tmp1 = ptrA++;
tmp2 = ptrB++;
*tmp1 = *tmp2;

in which case the increment happens first. 在这种情况下,增量首先发生。 Or the compiler can translate it into something like 或者编译器可以将其翻译成类似的东西

ptrA++;
ptrB++;
*(ptrA - 1) = *(ptrB - 1);

in which case the increment happens first as well. 在这种情况下,增量首先发生。

Once again, remember your interpretation of the original expression is good, but it is just one of the possible interpretations. 再一次,记住你对原始表达的解释是好的,但这只是可能的解释之一。 Never assume that things will happen in the specific order you used in your interpretation. 永远不要假设事情会按照您在解释中使用的特定顺序发生。

PS About those sequence points: C FAQ , C++ FAQ , Wikipedia PS关于这些序列点: C FAQC ++ FAQWikipedia

Yes, it does. 是的,它确实。 Because the code is using postfix operators: 因为代码使用的是后缀运算符:

Postfix operators are operators that are suffixed to an expression. Postfix运算符是表达式后缀的运算符。

operand++; 操作数++;

This causes the value of the operand to be returned. 这会导致返回操作数的值。 After the result is obtained, the value of the operand is incremented by 1. 获得结果后,操作数的值递增1。

You got it. 你说对了。 (15 char) (15个字符)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM