简体   繁体   English

有人可以在这里向我解释--destination的用法吗?

[英]Can someone explain me the use of --destination here?

I am completing my CISCO course on C and I got a doubt in the following function. 我正在完成有关C的CISCO课程,并且对以下功能有疑问。

Can someone please explain me the logic of the function, especially the use of --destination here? 有人可以解释一下该函数的逻辑,尤其是--destination吗?

char *mystrcat(char *destination, char *source) 
{
    char *res;
    for(res = destination; *destination++; ) ;
    for(--destination; (*destination++ = *source++); ) ;
    return res;
}

The first loop is looking for the string teminator. 第一个循环正在寻找字符串终止符。 When it finds it, with *destination being false, the pointer is still post-incremented with *destination++ . 当找到它时, *destination为false,则指针仍会通过*destination++后递增。

So the next loop starts by decrementing the pointer back to pointing to the '\\0' terminator, to start the concatentation. 因此,下一个循环通过将指针递减回到指向'\\0'终止符开始,以开始合并。

In the second loop, each character is copied until the string terminator is found with (*destination++ = *source++); 在第二个循环中,将复制每个字符,直到使用(*destination++ = *source++);找到字符串终止符为止(*destination++ = *source++); which is evaluated as the loop control. 评估为循环控制。 Again, this will include the required string terminator being copied. 同样,这将包括所需的字符串终止符被复制。

This is a very complicated function for something that shouldn't be written so difficult. 对于不应该编写那么困难的东西来说,这是一个非常复杂的功能。

--destination is a weird feature of C. I'm assuming you already know that variable++ increments the variable by one. --destination是C的一个怪异功能。我假设您已经知道variable++将变量加1。 Similarly variable-- decrements the variable by one. 同样, variable--variable--减1。 The thing is, when the ++ or -- comes after the variable name, that operation is done after the line is executed as a whole, when it is before the variable, C does the arithmetic first, then evaluates the full line. 关键是,当++或-出现在变量名之后时,该操作将在整体执行该行之后进行;当它在变量之前执行时,C首先进行算术运算,然后计算整行。

For an example: 例如:

int c = 5  
print(c++)  -> outputs '5'
print(c)    -> outputs '6'

but

int d = 5
print(++d) -> outputs '6'
print(d)   -> outputs '6'

This is because in the second example, the increment is evaluated before the entire line is evaluate. 这是因为在第二个示例中,在评估整行之前先评估增量。

Hope that helps. 希望能有所帮助。

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

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