简体   繁体   English

坚持使用 c 中的语法 - 指针

[英]Stuck with syntax in c - pointers

If we have a char *hello - and the string is "hello"如果我们有一个char *hello - 并且字符串是"hello"

and i do我做

char *ptr;
ptr = hello;

then ptr will be pointing at 'h' , correct?那么 ptr 将指向'h' ,对吗?

Now I have just done an assignmnet in this and completed it using the following terms现在我刚刚完成了一个assignmnet,并使用以下条款完成了它

if i wanted to move the pointer to the next chatachter i would just do ptr++ .如果我想将指针移动到下一个聊天者,我会做ptr++ If i wanted to use the value of the pointer for some check, i would use if(*ptr == '\\0')...如果我想使用指针的值进行一些检查,我会使用if(*ptr == '\\0')...

When i was doing the assignmnets our teacher gave us some pre built methods, and they used stuff like当我做作业时,我们的老师给了我们一些预先构建的方法,他们使用了诸如

*string++ = *s++;

ok, so why would we want to do *string (which gets a value) - and combine it with ++好的,那么为什么我们要执行*string (它获取一个值) - 并将其与++结合

I hope i make sense in explaining what is not clear.我希望我能解释不清楚的地方。 Its just I managed to do the whole assignment with ptr++ to move to next element or *ptr to check its value它只是我设法用 ptr++ 完成整个分配以移动到下一个元素或 *ptr 以检查其值

The idiom *s++ means "take the value pointed to, and switch to the next one".习语*s++意思是“取指向的值,然后切换到下一个”。 This way you can do your check operations in a loop.通过这种方式,您可以循环执行检查操作。 The assignment *p++ = *q++ copies the value of *q to the place pointed by p , and shifts both p and q to the next place, so the next time you execute *p++ = *q++ the next character will be copied behind the first one.分配*p++ = *q++拷贝的价值*q的地方指出由p ,和班次都pq下一个地方,所以在下一次执行*p++ = *q++的下一个字符的背后会被复制第一。 And so on.等等。

Not quite.不完全的。 In your original question, ptr would be set to point to the same first character as hello , which may not necessarily be h .在您的原始问题中, ptr将被设置为指向与hello相同的第一个字符,这可能不一定h You may have done:你可能做过:

char *hello = "goodbye";

in which case both hello and ptr will point to that g .在这种情况下helloptr都将指向那个g However your edit now makes it clear that you meant:但是,您的编辑现在清楚地表明您的意思是:

char *hello = "hello";

Your comment on ptr++ is correct.您对ptr++评论是正确的。 It will move ptr so that it points to the next character and, as per C string handling convention, '\\0' marks the end of the string.它将移动ptr使其指向下一个字符,并且按照 C 字符串处理约定, '\\0'标记字符串的结尾。

The statement:该声明:

*string++ = *s++;

is something you often see in string copying code (copying s to string ), something like:是您经常在字符串复制代码(将s复制到string )中看到的内容,例如:

char *mystrcpy (char *d, char *s) {
    char *d2 = d;
    while (*s != '\0')
        *d2++ = *s++;
    *d2 = '\0';
    return d;
}

In this case, the line *d2++ = *s++;在这种情况下,行*d2++ = *s++; means:方法:

  • copy the character at s to the memory location d2 .ss字符复制到内存位置d2
  • increment d2 to point to the next destination character.增加d2以指向下一个目标字符。
  • increment s to point to the next source character.增加s以指向下一个源字符。

In other words,换句话说,

*string++ = *s++;

is functionally identical to:在功能上等同于:

*string = *s;
string++;
s++;

so why would we want to do *string(which gets a value) - and combine it with ++那么为什么我们要做 *string(它得到一个值) - 并将它与 ++ 结合起来

When *string is on the left hand side of the equals, it doesn't get the value, it sets the value.*string位于等号的左侧时,它不会获取值,而是设置值。

The statement *string++ = *s++;语句*string++ = *s++; is equivalent to:相当于:

*string = *s;
s++;
string++;

This is because x++ is the postfix increment operator (as opposed to ++x which is the prefix increment operator).这是因为x++后缀增量运算符(而不是++x是前缀增量运算符)。 The pointer x is updated but the value that x was originally pointing to is used in the expression.指针 x 已更新,但在表达式中使用了x最初指向的值。

Personally I'd say that the one-liner is more confusing to read and you should generally try to avoid complex expressions with side-effects.就我个人而言,我会说 one-liner 读起来更令人困惑,您通常应该尽量避免具有副作用的复杂表达式。 But in this case it's a fairly standard idiom in C, so you might as well get used to it.但在这种情况下,它是 C 中相当标准的习语,因此您不妨习惯它。

then ptr will be pointing at h, correct?那么ptr将指向h,对吗?

Yes.是的。 No. See @paxdiablo's answer .不。请参阅@paxdiablo 的回答

*string++ = *s++;

This statement can be viewed as这个声明可以看作

*string = *s;
string ++;
s ++;

That means:这意味着:

  1. Copy the character in s to the location pointed by string .s的字符复制到string指向的位置。
  2. Move both s and string to the next character.sstring都移动到下一个字符。

then ptr will be pointing at h, correct?那么ptr将指向h,对吗?

Yes, correct.是,对的。

ok, so why would we want to do *string(which gets a value)好的,那我们为什么要做 *string(它得到一个值)

The '*' doesn't necessarily get a value: it may set a value, depending on which side of the '=' sign it is. '*' 不一定得到一个值:它可以设置一个值,这取决于它是 '=' 符号的哪一侧。

char *ptr;
ptr = "hello";
char firstLetter = *ptr; //get the value: now firstLetter contains 'h'
*ptr = 'w'; //set the value: now ptr is pointing to "wello".

Combining * with ++ means that you do one after the other:将 * 与 ++ 组合意味着您一个接一个地执行:

  • On the right-hand side, '*s++' means "get the value to which s is pointing, and then increment the s pointer".在右侧,'*s++' 表示“获取 s 指向的值,然后增加 s 指针”。
  • On the left-hand side, '*string++' means "set the value to which string is pointing, and then increment the string pointer".在左侧,'*string++' 表示“设置字符串指向的值,然后增加字符串指针”。

Combining them into a single statement simply a shorter, more compact (but functionally equivalent) way to write:将它们组合成一个简单的语句,以一种更短、更紧凑(但功能等效)的方式来编写:

*string = *s;
string++;
s++;

Just a quick addition to Mark Byers' answer, who correctly points out the postfix increment ("a++") as opposed to the prefix increment ("++a").只是对 Mark Byers 的回答的快速补充,他正确地指出了后缀增量(“a++”)而不是前缀增量(“++a”)。

You probably want to have a look at the C operator precedence for the order in which operators are handled in an expression.您可能想查看C 运算符优先级以了解运算符在表达式中的处理顺序。

You can always force the precedence you want by using parentheses: (a + b) * c != a + b * c.你总是可以通过使用括号来强制你想要的优先级:(a + b) * c != a + b * c。

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

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