简体   繁体   English

不了解++和-的指针算法

[英]Not Understanding Pointer Arithmetic with ++ and --

So, I am learning about pointers via http://cplusplus.com/doc/tutorial/pointers/ and I do not understand anything about the pointer arithmetic section. 因此,我正在通过http://cplusplus.com/doc/tutorial/pointers/了解有关指针的知识,而我对指针算术部分一无所知。 Could someone clear things up or point me to a tutorial about this that I may better understand. 有人可以解决问题,还是可以给我指向一个我可能会更好理解的教程。

I am especially confused with all the parentheses things like the difference between *p++ , (*p)++ , *(p++) , and etc. 我特别对所有括号感到困惑,例如*p++(*p)++*(p++)等之间的区别。

*p++

For this one, ++ has higher precedence then * so it increments the pointer by one but retrieves the value at the original location since post-increment returns the pointer and then increments its value. 对于这一点, ++优先级比*因此它将指针加1,但由于后递增返回指针然后递增其值,所以它会检索原始位置的值。

(*p)++

This forces the precedence in the other direction, so the pointer is de-referenced first and then the value at that location in incremented by one (but the value at the original pointer location is returned). 这将强制优先顺序在另一个方向上,因此首先取消对指针的引用,然后将该位置的值加1(但将返回原始指针位置的值)。

*(p++)

This one increments the pointer first so it acts the same as the first one. 该指针首先递增指针,因此其作用与第一个指针相同。

An important thing to note, is that the amount the pointer is incremented is affected by the pointer type. 需要注意的重要一点是,指针增加的数量受指针类型的影响。 From the link you provided: 通过您提供的链接:

char *mychar;
short *myshort;
long *mylong;

char is one byte in length so the ++ increases the pointer by 1 (since pointers point to the beginning of each byte). char是一个字节的长度,因此++会将指针增加1(因为指针指向每个字节的开头)。

short is two bytes in length so the ++ increases the pointer by 2 in order to point at the start of the next short rather than the start of the next byte. short是两个字节的长度,因此++会将指针增加2,以指向下一个short的开头而不是下一个字节的开头。

long is four bytes in the length so the ++ increases the pointer by 4. long是长度的四个字节,因此++将指针增加4。

I found useful some years ago an explanation of strcpy, from Kernighan/Ritchie (I don't have the text available now, hope the code it's accurate): cpy_0, cpy_1, cpy_2 are all equivalent to strcpy: 几年前,我发现对Kernighan / Ritchie的strcpy解释很有用(我现在没有文本,希望代码是准确的):cpy_0,cpy_1,cpy_2都等同于strcpy:

char *cpy_0(char *t, const char *s)
{
    int i = 0;
    for ( ; t[i]; i++)
        t[i] = s[i];
    t[i] = s[i];
    i++;
    return t + i;
}
char *cpy_1(char *t, const char *s)
{
    for ( ; *s; ++s, ++t)
        *t = *s;
    *t = *s;
    ++t;
    return t;
}
char *cpy_2(char *t, const char *s)
{
    while (*t++ = *s++)
        ;
    return t;
}
*p++

Returns the content, *p , an then increases the pointer's value (postincrement). 返回内容* p ,然后增加指针的值(后递增)。 For example: 例如:

int numbers[2];
int *p;
p = &numbers[0];
*p = 4;        //numbers[0] = 4;
*(p + 1) = 8;  //numbers[1] = 8;
int a = *p++;  //a = 4 (the increment takes place after the evaluation)
               //*++p would have returned 8 (a = 8)
int b = *p;    //b = 8 (p is now pointing to the next integer, not the initial one)

And about: 关于:

(*p)++

It increases the value of the content, *p = *p + 1; 它增加了内容的值, * p = * p +1; .

(p++); //same as p++

Increases the pointer so it points to the next element (that may not exist) of the size defined when you declared the pointer. 增加指针,使其指向声明指针时定义的大小的下一个元素(可能不存在)。

First you have to understand what post increment does; 首先,您必须了解职位增量的作用;
The post increment, increases the variable by one BUT the expression (p++) returns the original value of the variable to be used in the rest of the expression. 后递增,使变量增加一个, 表达式(p ++)返回要在表达式的其余部分中使用的变量的原始值。

char   data[] = "AX12";
char* p;

p = data;
char* a = p++;

// a -> 'A'  (the original value of p was returned from p++ and assigned to a)
// p -> 'X'

p = data;   // reset;
char  l =  *(p++);

// l =  'A'. The (p++) increments the value of p. But returns the original
             value to be used in the remaining expression. Thus it is the
             original value that gets de-referenced by * so makeing l 'A'
// p -> 'X'

Now because of operator precedence: 现在由于运算符优先级:

*p++ is equivalent to *(p++)

Finally we have the complicated one: 最后我们有一个复杂的例子:

p = data;
char m = (*p)++;

// m is 'A'. First we deference 'p' which gives us a reference to 'A'
//           Then we apply the post increment which applies to the value 'A' and makes it a 'B'
//           But we return the original value ('A') to be used in assignment to 'm'

// Note 1:   The increment was done on the original array
//           data[]  is now "BXYZ";
// Note 2:   Because it was the value that was post incremented p is unchaged.
// p -> 'B' (Not 'X')

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

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