i am using c pointers in which if i increment as *p++ it increments p but returns only the value which p pointed before it was incremented. how can i increment the value pointed by p.
do this: (*p)++
Using parenthesis, you've indicated you mean to increment the value pointed at.
postfix ++ has a higher precedence than *, the compiler reads *p++ as *(p++). Since you want to increment the value of *p, you need braces: (*p)++ will return the value of *p and afterwards increment the value of p by one. ++(*p) will increment the value of *p and return this value then.
It is possible you omit the braces for the last case and write ++*p, but I'd suggest not to do it, because ++(*p) is the dual to (*p)++, but for ++*p it is not *p++.
Related
The C operator precedence chart looks like this:
Why is post increment/decrement first on the list but *p++ results in dereferencing a pointer first and then incrementing adress it points to?
*p++ parses as *(p++).
the value of p++ is p (and the side effect is incrementing p), so the value of *p++ is the same value as *p.
Except for the side-effect, *p and *p++ are identical.
The exact same thing happens with integer:
int n = 6;
printf("%d\n", 7 * n);
printf("%d\n", 7 * n++); // except for the side-effect same value as above
In am doing some experiment in C pointers and trying to understand its behaviour. The following are my assumptions for the below codes. Correct me if I am wrong.
I have the following codes:
int n[5] = {3,6,9,12,15};
int *ptr = n;
ptr++;
printf("%d", *ptr); //Needless to say, the output will be 6
My assumption: The output above is 6 because ptr++ means ptr = ptr + 1 I am changing the value of ptr which is the address of n[0].
Now we take a look at the following scenario:
int n[5] = {3,6,9,12,15};
int *ptr = n;
*ptr++;
printf("%d", *ptr); //Why is the output still 6?
My question is: How do we interpret *ptr++? Does it means:
*ptr = *ptr + 1 or
*ptr = ptr + 1 or
ptr = ptr + 1 or what?
By the way, when I print out values of n, it is still 3,6,9,12,15.
Sorry for 2nd question:
How shall we interpret *++ptr and ++*ptr then?
Expression
*ptr++;
have value
*ptr
before incrementing ptr and then ptr is increment itself. There is no sense to write
*ptr++;
because the value of expression that is *ptr before incrementing of ptr is not used. So in fact the result of these expressions (expression-statements)
ptr++;
and
*ptr++;
is the same.
As for expressions *++ptr and ++*ptr then in this expression *++ptr at first ptr is incremented ( that is it will point to the second element of the array) and then dereferenced and its value is the value of the second element.
In this expression ++*ptr at first the value of the first element of the array is returned (that is 3) and then this value is incremented and you will get 4.
Please check the link below:
Difference between ++*argv, *argv++, *(argv++) and *(++argv)
You need to know about operator precedence in-order to understand what is going on here.
++ operator has higher precedence over *
Both ptr++ and *ptr++ increment the pointer after they have returned, in the first case, the previous address to which ptr was pointing, and in the second case, the value from this address. You do not do anything with the results, so you do not see the difference.
*++ptr will first increment ptr, then returns the value to which it now points.
++*ptr will get the value to which ptr points, increment it, and then return.
The order of precedence is also compiler dependent. It may change depending upon compilers. Better use a parenthesis to be sure of output
It is due to operator precedence in C. The below link may help you
Increment or decrement operator has higher precedence that dereference operator.So your
*ptr++; is similar to *(ptr++)
http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm
Because of the operator precedence *pt++; is the same as *(ptr++); so it increments the pointer, then dereference the pointer and does nothing.
Please help me understand the reason the following code works the way it does:
#include <stdio.h>
int main(){
int a = 10;
void *b = &a;
int *p = b;
printf("%u",*p++);
return 0;
}
I know the output of printf will be 10, but I'm not quite following why *p++ is 10
Here are my steps:
1) void *b = &a; stores the address of a in pointer b
2) int *p = b; pointer p now points to the same data item as pointer b
3) printf("%u",*p++); is where I get confused... the dereference of pointer p is a, which is 10... isn't *p++ basically the same as 10+1 which will be 11?
*p++ is essentially *(p++). It evaluates to the value of p before it is incremented which is the address to a. Then you dereference it which evaluates to the value 10.
The post-increment operator in the expression *p++ applies to the pointer, not the value stored at that location, so the result is never 11, before or after it is evaluated. The expression *p++ means: dereference p (get it's value) then increment p one location. Since p points to an int, incrementing it will move it forward sizeof(int) bytes . The addition does not ever apply to the value that p points to, which is 10.
However, the expression (*p)++ is different. It dereferences p (gets its value) and then increments the value in that memory location. The expression evaluates to the original value. So after executing the statement
int c = (*p)++;
the variable c would equal 10, while a would equal 11.
*p++ is parsed as *(p++). p++ evaluates to p, and then increments p, so the change won't be seen until the next reference to p. So *p is 10, *p++ is 10 (but p now points to &a+1), *++p is undefined behavior (because *(&a+1) is not a valid value), (*p)++ is 10 but changes a to 11, and ++*p (or ++(*p)) is 11 (as is a).
Variable p is a pointer to an int (pointing to a)
The expression *p dereferences the pointer, hence it's like accessing the int a directly.
Operator postfix ++ on pointer p takes precedence over the dereferencing. Therefore *p++ increments the pointer p (to whatever junk is in memory after int a) AFTER the expression is evaluated, so the dereferencing still resolves to a and that's why 10 is printed. But after the statement is run the value of p is changed. So, likely after that statement if you do printf("%u ",*p) you will get an awkward value.
If you do ++*p however, the expression is evaluated as ++ operation on the dereferenced int variable pointed by p. If you want to avoid trouble like this, when not sure, use parenthesis:
(*p)++
++(*p)
And you're making sure you are dereferencing the value and acting on it. Incrementing a pointer value is a very dangerous operation allowed by languages like C and C++, so avoid whenever possible!
Why *p++ is 10 ?
[C11: ยง6.5.2.4/2]: The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).
The below statement
printf("%u",*p++);
is equivalent to
printf("%u",*p); /* p points to 'a' and value of `a` is 10. Hence, 10 is printed */
p = p + 1;
p is of type pointer-to-int. Hence, 1 is scaled to sizeof (int).
As a result, p now points to an int at address : p + sizeof (int)
I just want to add my five cents.
For incrementing value indirected via pointer you can use ++*ip or (*ip)++. There is a nice explanation about parentheses in K&R book:
The parentheses are necessary in this last example
(*ip)++; without them, them expression
would increment ip instead of what it points to, because
unary operators like * and ++ associate right to left.
And in your piece of code you got 10 because printf will print original value of variable and only after it wiil be incremented by one due of using of postfix ++ operator.
My CS course is taking a bit of a turn from Java to C. I'm busy going over pointers at the moment and I have come across that the ++ operator for incrementing doens't work when dereferencing. This is more just a curiosity question than anything else. Just not used to the pointers concept just yet. Am I just doing something wrong or is it something to do with pointers?
For example:
*pointer++; Will not increment the value.
*pointer+=1; Will increment the value.
Thanks in advance!
When you want to increment the value you have to make sure you use parenthesis.
(*pointer)++;
*pointer++;
is equivalent to
*(pointer++); // pointer is incremented
and not to
(*pointer)++; // pointee is incremented
*pointer++ increments the pointer variable, not value pointed by it.
int array[4] = {3,5,7,9};
int *pointer = array;
// *pointer equals 3
*pointer++;
// *pointer now equals 5
This has to do with precedence of the operators: post-increment ++ has higher precedence than the dereference operator *, while += has lower precedence in the table of operator precedences. That is why in the first example ++ is applied to the pointer which is dereferenced afterwards, while in the second example += 1 is applied to the result of dereference.
*pointer++; is almost equivalent to:
*pointer;
pointer = pointer + 1;
Why its so?
In expression *pointer++;, ++ is postfix operator, so fist * deference operation performed then ++ increments value of pointer (and not increments value).
Whereas *pointer += 1 is just equivalent to:
*pointer = *pointer + 1;
that increments value pointed by pointer.
#include <stdio.h>
int main()
{
char a[] = "hello";
char *ptr = a;
printf ("%c\n",*ptr++);//it prints character 'h'.
printf ("%c\n",*ptr);//it prints character 'e'.
return 0;
}
As I understand it: In the above code, in *ptr++ expression, both * and ++ have same precedence and operation will take place from right to left, which means pointer will increment first and deference will happen next. So it should print the character 'e' in the first printf statement. But it is not.
So my question is: Where will it store the incremented value (in, *ptr++) if it is not dereferencing that location in first printf statement?
ptr++ means "increment ptr, but return the pre-increment value."
Thus despite the fact that the increment happens first, it is the original, non-incremented pointer that is being dereferenced.
By contrast, if your precedence reasoning is correct, *++ptr should print e as you expect. ++ptr means "increment ptr and return the post-increment value".
Whatever happens is correct.
When doing *ptr ++ it just takes the *ptr value and performs operation as it is a post increment and had you used ++ *ptr it would have printed e in the very first place.
p++ is post increment while ++p is pre increment. p++ gives the values of p and then increments the contents of p while ++p increments the contents of p and then returns the value of p
It will be stored in the pointer ptr itself. It is like making the ptr point to the next byte that it used to point:
ptr = ptr + 1;
yupp, pointer will be autoamticaly incremented after each use.