Is ++ the same as += 1 for pointers?

谁都会走 提交于 2019-12-10 13:22:26

问题


I'd like to refactor some old C code of mine, and I was curious if I can replace all ptr++ with ptr += 1 where ptris some pointer, without changing any behavior. Here's an example of what I mean, from K&R Section 5.3:

/* strlen: return length of string s*/
int strlen(char *s)
{
    int n;
    for (n = 0; *s != '\0'; s++)
        n++;
    return n;
}

When I replace the s++ with s += 1, I get the same results, but I'm wondering if this will be the case for all types. I also did a test for ints:

int size = 10;
int *int_array = (int*)calloc(size, sizeof(int));
for (int i = 0; i < size; i++)
    int_array[i] = i;

for (int i = 0; i < size; i++) {
    printf("*int_array = %d\n", i, *int_array);
    int_array++;
}

If I replace the line int_array++; with int_array += 1;, I get the same result.

After thinking about this some more, I realize there could be a problem if the value is used in an expression. Would it be safer I just moved the increment onto another line like so:

int a = 5;
int b = a++;

would become:

int a = 5;
int b = a;
a += 1;

Conclusion

What I thought could be a problem, incrementing pointers of different types, is not a problem. See @bdonlan's response for the reason why.

This doesn't mean that you can replace all x++ with x += 1 and expect the same behavior. You can, however, replace ++x with (x += 1) safely, since they are equivalent.


回答1:


a += 1 is equivalent to ++a (C99 §6.5.3.1/2). In a line like int b = a++; this means it is not equivalent to a++; a++ would return the old value of a, while a += 1 returns the new value.

Note that if you don't use the result of a++ (ie, you have a statement containing just a++;), then they are effectively identical.

Also, note that _all pointer arithmetic is done in increments of the pointed-to type's size (§6.5.6/8). This means that:

ptr = ptr + x;

is equivalent to:

ptr = (ptr_type *)( (char *)ptr + x * sizeof(*ptr) );

This is the same whether you use +, ++, +=, or [] (p[x] is exactly equivalent to *(p + x); you can even do things like 4["Hello"] because of this).




回答2:


++ and -- is defined in terms of arithmetic for built-in types. The behavior will be the same, other than postfix returning the old value.




回答3:


It's a good question. The answer is yes, you can do that -- no matter how you do it, incrementing a pointer adds sizeof(the type that the pointer points to) to the pointer. As other answers indicate, you do need to be careful that you don't depend on when the increment happens, i.e. a++ and ++a have different effect, but a eventually ends up with the same value.

A question is: why do you want to change all your code from a++ to a+=1? Using the post-increment operator with pointers is something that should be easily understood by any C programmer, so it's hard to understand why you'd make that change.



来源:https://stackoverflow.com/questions/8421041/is-the-same-as-1-for-pointers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!