++ operator in the case of pointers
-
I have some basic knowledge about pointers and I`m trying to get an exhaustive understanding, like an all sides perspective. My question is what does the operator ++ do to a pointer? I`ve seen it being used in linked lists related code.
-
It is the increment operator, and as with any variable it adds 1 to the pointer. This will move the pointer forward by one element of whatever it is pointing to: int, long, struct, object etc.
could I increment not the value of the variable the pointer is pointing to but the pointer itself?
int Something0 = 0;
int Something1 = 1;
int ** Pointers = new int*[2];
Pointers[0] = &Something0;
*Pointers[0]++; //increase Something0 by 1
Pointers[1] = &Something1;
*Pointers[1]++; //increase Something1 by 1//Could I do this
**Pointers =10; //set Something0 to 10
Pointers++;
**Pointers =30; //set Something1 to 30 -
could I increment not the value of the variable the pointer is pointing to but the pointer itself?
int Something0 = 0;
int Something1 = 1;
int ** Pointers = new int*[2];
Pointers[0] = &Something0;
*Pointers[0]++; //increase Something0 by 1
Pointers[1] = &Something1;
*Pointers[1]++; //increase Something1 by 1//Could I do this
**Pointers =10; //set Something0 to 10
Pointers++;
**Pointers =30; //set Something1 to 30That's what the increment operator does to a pointer - it increments the pointer itself. If you want to increment the value that it's pointing to, you must dereference the pointer first, like so:
(*pointer)++;
The difficult we do right away... ...the impossible takes slightly longer.
-
That's what the increment operator does to a pointer - it increments the pointer itself. If you want to increment the value that it's pointing to, you must dereference the pointer first, like so:
(*pointer)++;
The difficult we do right away... ...the impossible takes slightly longer.
Thanks I think I understand.
-
I have some basic knowledge about pointers and I`m trying to get an exhaustive understanding, like an all sides perspective. My question is what does the operator ++ do to a pointer? I`ve seen it being used in linked lists related code.
You should also know about the difference between a
pre-increment
andpost-increment
operator. In an expression, a pre-increment operator computes the increment before accessing the item, and a post-increment computes the increment afterwards. For exampleint i = 1;
int j = ++i; // i is incremented before assignment: i = 2, j = 2;
int k = i++; // i is incremented after assignment: i = 3, k = 2;
int x = ++i++; // UNDEFINED BEHAVIOR!!! Do not do this!!!In the case
++i++
The standard does not say what will happen. The only thing you know is that i will have the value 5 after the expression is complete. The variable x could be assigned at any point during the expression, or since this is undefined behavior, it might even get the value -42, which makes no sense in respect to the code, but is acceptable according to C/C++ standard. Even if you do some testing and work out that for typeint
the expression++i++
is the equivalent of++(i++)
, it might be different for typelong
, or for a pointer type, and it might change depending on complexity of the overall expression, and/or the optimization level. The takeaway for this is that you don't know what will happen if you try to pre and post increment an expressions term at the same time. Moreover, you should not try to increment the same variable within the same expression:int i = 2;
printf("%d %d\n", i++, ++); // undefined order of evaluationIn the printf statement above, its undefined which order the arguments to printf get evaluated, and different compilers do it differently. I'm reasonably sure that clang and gcc do this differently.
Keep Calm and Carry On
-
You should also know about the difference between a
pre-increment
andpost-increment
operator. In an expression, a pre-increment operator computes the increment before accessing the item, and a post-increment computes the increment afterwards. For exampleint i = 1;
int j = ++i; // i is incremented before assignment: i = 2, j = 2;
int k = i++; // i is incremented after assignment: i = 3, k = 2;
int x = ++i++; // UNDEFINED BEHAVIOR!!! Do not do this!!!In the case
++i++
The standard does not say what will happen. The only thing you know is that i will have the value 5 after the expression is complete. The variable x could be assigned at any point during the expression, or since this is undefined behavior, it might even get the value -42, which makes no sense in respect to the code, but is acceptable according to C/C++ standard. Even if you do some testing and work out that for typeint
the expression++i++
is the equivalent of++(i++)
, it might be different for typelong
, or for a pointer type, and it might change depending on complexity of the overall expression, and/or the optimization level. The takeaway for this is that you don't know what will happen if you try to pre and post increment an expressions term at the same time. Moreover, you should not try to increment the same variable within the same expression:int i = 2;
printf("%d %d\n", i++, ++); // undefined order of evaluationIn the printf statement above, its undefined which order the arguments to printf get evaluated, and different compilers do it differently. I'm reasonably sure that clang and gcc do this differently.
Keep Calm and Carry On
thanks k5054. I`m not sure I fully understand the difference. I guess I need look up the issue in several different sources. (I often find consulting different perspectives on the same issue helpful).
-
thanks k5054. I`m not sure I fully understand the difference. I guess I need look up the issue in several different sources. (I often find consulting different perspectives on the same issue helpful).
-
You should also know about the difference between a
pre-increment
andpost-increment
operator. In an expression, a pre-increment operator computes the increment before accessing the item, and a post-increment computes the increment afterwards. For exampleint i = 1;
int j = ++i; // i is incremented before assignment: i = 2, j = 2;
int k = i++; // i is incremented after assignment: i = 3, k = 2;
int x = ++i++; // UNDEFINED BEHAVIOR!!! Do not do this!!!In the case
++i++
The standard does not say what will happen. The only thing you know is that i will have the value 5 after the expression is complete. The variable x could be assigned at any point during the expression, or since this is undefined behavior, it might even get the value -42, which makes no sense in respect to the code, but is acceptable according to C/C++ standard. Even if you do some testing and work out that for typeint
the expression++i++
is the equivalent of++(i++)
, it might be different for typelong
, or for a pointer type, and it might change depending on complexity of the overall expression, and/or the optimization level. The takeaway for this is that you don't know what will happen if you try to pre and post increment an expressions term at the same time. Moreover, you should not try to increment the same variable within the same expression:int i = 2;
printf("%d %d\n", i++, ++); // undefined order of evaluationIn the printf statement above, its undefined which order the arguments to printf get evaluated, and different compilers do it differently. I'm reasonably sure that clang and gcc do this differently.
Keep Calm and Carry On
-
I am guessing that
k5054 wrote:
int i = 2;
printf("%d %d\n", i++, ++); // undefined order of evaluationshould have read as
int i = 2;
printf("%d %d\n", i++, ++i); // undefined order of evaluation -
You should also read Why Does x = ++x + x++ Give Me the Wrong Answer?[^].
Thanks