Are these examples correct? - c

In this youtube video, I came across with the following examples:
However, they doesn't seem correct to me, for the first example:
*ptr++
In my understanding, this means that, if ++ has greated precedence than *, increment the pointer by 1 in pointer arithmetic, then get the value stored in the address which is pointed by this new pointer. So the code
x = *ptr++;
should be equal to,
ptr++;
x = *ptr;
Which is either conflicting with the video, or I am mixing somethings. Can you please tell me if I am right or wrong?

Precedence affects how expressions are structured. It does not affect what the rules of the operations are.
In *ptr++, precedence says the rules for ++ are applied to ptr, and then the rules for * are applied to the result.
The rules for ++ say that, with ptr++, the resulting value is that of ptr when the operation starts and, separately, the value of ptr is incremented. The actual increment of ptr in memory may occur before, during, or after the production of the result value, but it does not affect the value: The value produced is always the value of ptr before the increment, regardless of when the increment actually occurs.
The rules for * say the result is the object that its operand points to. So, with *ptr++, the result is the object that ptr points to before the increment is applied.

Each expression if it is not a void expression has a value.
The value of the postfix increment is the value of its operand before incrementing.
From the C Standard (6.5.2.4 Postfix increment and decrement operators)
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)
That is this expression
*ptr++
is evaluated like
*( ptr++ )
and the value of the expression in the parentheses is the value of the pointer (the operand of the postfix increment) before incrementing of the operand itself that is evaluated as a side effect.
This is the difference between the postfix increment and the prefix increment for which (6.5.3.1 Prefix increment and decrement operators)
2 The value of the operand of the prefix ++ operator is incremented.
The result is the new value of the operand after incrementation.
It is useful to know that the postfix increment operator may be used to form a subscript expression while the prefix increment operator may not.
Here is a demonstrative program.
#include <stdio.h>
int main(void)
{
int a[] = { 1, 2 };
int *p = &a[0];
printf( "a[0] = %d, a[1] = %d\n", a[0], a[1] );
p++[0] = 3;
*p = 4;
printf( "a[0] = %d, a[1] = %d\n", a[0], a[1] );
return 0;
}
The program output is
a[0] = 1, a[1] = 2
a[0] = 3, a[1] = 4

Related

Order if Increment and Evaluation of Array in C with Shorthand Notation

I have written a small code to understand the order of increment and evaluation of an array with the shorthand increment notation within the array:
#include <stdio.h>
int j;
int array[500];
void main() {
array[0]=3;
array[1]=3;
array[2]=3;
while (j<10){
array[j++]=7;
printf("value of array[j] after %d\n",array[j]);
}
}
It appears that the increment is occurred "after" the evaluation of the array so array[j] is assigned 7 and then j is incremented. Is it always the case the evaluation happens before the increment in such shorthand array[j++] notations?
The result of the postfix ++ is the original value of its operand, and the operand is incremented as a side effect.
This is in contrast to the prefix ++ operator:
array[++j]=7;
Which evaluates to the incremented value of its operand, and again the operand is incremented as a side effect.
In both cases, the side effect of incrementing is not guaranteed to happen until the next sequence point, which loosely speaking means the next statement.
That means if you attempt to do something like this:
x = ++j + j;
It causes undefined behavior because j is being read and written in the same statement.

differences in C: *source++, (*source)++, *(source)++

IS there a difference between these pointers? What exactly is happening here for each call.
*p++
(*p)++,
*(p)++
1 and 3 are the same.
Remember that both postfix and unary forms of ++ and -- have a result and a side effect:
The result of x++ is the current value of x - as a side effect, x is incremented by 1 (if x is a pointer, it is incremented to point to the next object in a sequence);
The result of ++x is the current value of x plus 1 - as a side effect, x is incremented by 1 (if x is a pointer, the result is the address of the next object in a sequence, and x is updated to point to the next object in a sequence);
Both forms of -- work the same way, except the value is decremented by 1 - if it's a pointer, then it's set to point to the previous object in a sequence.
When you throw pointer dereferences into the mix, you get the following:
The expression *p++ is parsed as *(p++) (so is *(p)++). The result of *p++ is the current value of *p (the value of the thing p is currently pointing to). As a side effect, p is incremented to point to the next object of the same type in a sequence (IOW, the next element of an array);
The expression (*p)++ is parsed as written. The result of (*p)++ is the current value of *p. As a side effect, *p is incremented by 1. That is, the value of the thing being pointed to is updated, not the pointer.
The expression ++*p is parsed as ++(*p). The result of ++*p is the current value of *p plus 1. As a side effect, *p is incremented by 1.
The expression *++p is parsed as *(++p). The result of *++p is the value of the object following the object p currently points to. As a side effect, p is incremented to point to the next object.
Assume the following declarations:
int a[] = {1, 2, 3, 4};
int *p = a;
After these lines, the value of p is &a[0]. So, given the expression
x = *p++;
the result of *p++ is 1 (the value of the thing p currently points to), which gets assigned to x. The side effect is that p is updated to point to a[1].
Then we execute
x = (*p)++;
The result of (*p)++ is the value of the thing p currently points to (if p points to a[1], then the value is 2), which gets assigned to x. As a side effect, the thing p points to is incremented (if p points to a[1], then the value of a[1] is now 3).
The we execute
x = ++*p;
The result of ++*p is the value of the thing p points to plus 1, and as a result the thing p points to is incremented (if p points to a[1], then the value of a[1] + 1 is 4, which is assigned to x, and the value of a[1] is now 4).
Finally, we execute
x = *++p;
The result of *++p is the value of the object following the object that p currently points to, and p is incremented to point to that object (if p points to a[1], then the value of a[2] (3) is written to x, and p is updated to point to a[2]).
Again, -- works the same way, just in the other direction.
tiny test program.
#include <stdio.h>
int main(void) {
char z[] = "World";
char n[] = "Something";
char *x = "Hello!!";
while(*x) printf("%c", *x++);
printf("\n");
x = z;
while(*x) printf("%c", (*x)++);
printf("\n");
x = n;
while(*x) printf("%c", *(x)++);
printf("\n");
return 0;
}
so the *x++ dereference the pointer, then increments the pointer
(*x)++ - only increments the referenced object.
*(x)++ == *x++
IMO instead of asking try yourself. You will learn something https://ideone.com/bliza0
*source++ will be parsed as *(source++) because of higher precedence of ++ over * operator. It will increment the pointer by 1 and then will dereference the pointer.
To understand the behaviour first let me explain how post increment operator works. When you do
int i = 0;
int j = i++;
then it means that the value of the expression i++ will be the value of the operand i. In this case it will be 0. The value of the i will be incremented as a side-effect. See the quote from the draft
n1570 - §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). See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers. The value computation of the result is sequenced before the side effect of updating the stored value of the operand.
In case of *source++, compiler will parse it as *(source++). The result of the expression source++ is the value of source. So the current pointer (the address source is pointing to before the side-effect) will be dereferenced and then the side-effect on source will be performed.
(*source)++ will first dereference the pointer and then increment the dereferenced value by 1.
*(source)++ is equivalent to the first case.
I apologies for the wrong answer and leaving the wrong answer with strikes so that future readers will take a lesson if any one come to the same wrong conclusion as I did. Thanks #interjay and #pmg for correcting me out. I am really glad that you guys are part of this community and contributing to this community with your sharp eyes and mind.

C language basics 1

What is the difference between a++ and a=a+1 in C if a is a variable.
Tell me the difference between the following assignments;
x=a+1;
x=a++;
I am not able to find a convincing explanation on Google.Please explain the difference clearly and step by step.
a = a+1; calculates the result of a+1 and assigns it to a.
a++; increments a by 1, but evaluates to the previous value of a.
++a; increments a by 1, and evaluates to the new value of a.
a += 1 is identical to ++a;.
So for example:
x = a++; → a will be incremented by 1, and the previous value of a will be assigned to x.
x = ++a; → a will be incremented by 1, and the new value of a will be assigned to x.
x=a+1 just sets the value of x.
x=a++ first sets x to the value of a, and then increments a. a++ is an operation in itself, and you could call it as a single statement to increment a, without assigning it to x whatsoever.
FYI, there is also ++a, which could be used like this:
x=++a. In this case, a is incremented first, and then the incremented value is assigned to x. In either case, the ++ operator modifies a, which is not the case for a+1.
From the C11 Standard (draft):
6.5.2.4 Postfix increment [...] operator[...]
[...]
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).
So
int a = 0;
int x = a++;
results in a being 1 and x being 0.
x = a++ returns the value of a to the variable x first and later it is incremented.
x = a+1 Here a+1 is evaluated and the result is stored in x
3 differences
Return value and side effect
// a is incremented, but its former value is assigned to x
x=a++; //
// a is unchanged. The sum of `a+1`is assigned to x
x=a+1;
Operator precedence. post-fix ++ (aka Suffix increment) is higher precedence than simple addition.
++ only works with integer and non-void* pointer types. +1 works with all scalar type including double.
int a;
x=a++; // valid
x=a+1; // valid
double a;
x=a++; // invalid
x=a+1; // valid

Why is the -- operator not subtracting from the value when executed? [duplicate]

This question already has answers here:
What is the difference between prefix and postfix operators?
(13 answers)
Closed 8 years ago.
Why is the decrement operator -- not bringing the value down by 1 when executed?
int a = 20;
int c ;
c = a--;
Inspecting the value of c now, it should be 19, yet it comes out as 20. What am I missing?
a-- is Post-Decrement, what you need --a Pre-Decrement. Please read Increment and decrement operators on Wiki
The following C code fragment illustrates the difference between the pre and post increment and decrement operators:
int x;
int y;
// Increment operators
x = 1;
y = ++x;
// x is now 2, y is also 2
y = x++;
// x is now 3, y is 2
// Decrement operators
x = 3;
y = x--;
// x is now 2, y is 3
y = --x;
// x is now 1, y is also 1
what you're using is called a postfix operator. It will get executed [decrement the value] after the assignment = operator has finished its execution with the existing value.
To be clear, in case of post decrement, the ..-- operator is evaluated and the decrement is scheduled once the other evaluations including that operand are finished. It means, the existing value of the operand is used in the other evaluation [in =] and then the value is decreased.
If you want, try printing the value of a itself. It will print the decremented value.
EDIT:
If my choice of words in my answer created any confusions, for the reference, from the c99 standard, chapter 6.5.2.4, [emphasis mine]
[For increment] The result of the postfix ++ operator is the value of the operand. After the result is obtained, the value of the operand is incremented [......]
The postfix -- operator is analogous to the postfix ++ operator, except that the value of the operand is decremented (that is, the value 1 of the appropriate type is subtracted from it).
You are using the post decrement. Post decrement means first use the value in a variable or
anything then decrement the value in the variable. So in this case first value of a will assigned to c. And the decrement is done. You can check printing the value of a.
you should use --a (pre decrement operator), you are using post decrement operator a--
The result of the postfix -- operator is the value of the operand. As a side effect, the
value of the operand object is decremented (that is, the value 1 of the appropriate type is
subtracted to it).

Post-increment in a while loop

The following code confused me a bit:
char * strcpy(char * p, const char * q) {
while (*p++=*q++);
//return
}
This is a stripped down implementation of strcpy function. From this code, we see that pointer p and q are incremented then dereferenced and q is assigned to p until the \0 char has been reached.
I would like someone to explain the first iteration of the while loop.
Because the ++ is after the variables, they aren't incremented until after the expression is evaluated. That's why it's the post-increment operator; the pre-increment is prefixed (++p). *++p would write to the second spot, *p++ writes to the first.
No, the increment happens after the assignment.
If it were *(++p), the pointer p would be incremented and after that assigned.
p++ is post-incrementing the pointer p. So the current value of p is operated upon by the deference operator * before p is incremented.
Your reasoning would've been correct if the while loop was written as follows:
while (*++p=*++q);
In this case the increment would happen before dereferencing.
The expressions x++ and ++x have both a result (value) and a side effect.
The result of the expression x++ is the current value of x. The side effect is that the contents of x are incremented by 1.
The result of the expression ++x is the current value of x plus 1. The side effect is the same as above.
Note that the side effect doesn't have to be applied immediately after the expression is evaluated; it only has to be applied before the next sequence point. For example, given the code
x = 1;
y = 2;
z = ++x + y++;
there's no guarantee that the contents of x will be modified before the expression y++ is evaluated, or even before the result of ++x + y++ is assigned to z (neither the = nor + operators introduce a sequence point). The expression ++x evaluates to 2, but it's possible that the variable x may not contain the value 2 until after z has been assigned.
It's important to remember that the behavior of expressions like x++ + x++ is explicitly undefined by the language standard; there's no (good) way to predict what the result of the expression will be, or what value x will contain after it's been evaluated.
Postfix operators have a higher precedence than unary operators, so expressions like *p++ are parsed as *(p++) (i.e., you're applying the * operator to the result of the expression p++). Again, the result of the expression p++ is the current value of p, so while (*p++=*q++); doesn't skip the first element.
Note that the operand to the autoincrement/decrement operators must be an lvalue (essentially, an expression that refers to a memory location such that the memory can be read or modified). The result of the expression x++ or ++x is not an lvalue, so you can't write things like ++x++ or (x++)++ or ++(++x). You could write something like ++(*p++) (p++ is not an lvalue, but *p++ is), although that would probably get you slapped by anyone reading your code.
The right hand side of the expression (*q++) will be evaluated prior to *p++, and both will only be incremented after the assignment takes place.
Read the statement right to left and remember post-increment (q++ instead of ++q) happens after everything else in the line is resolved.
*q --> dereference q
= --> assign the value
*p --> to p
increment both.
Do this until q p taking q's element = 0 which is when it reaches the null terminator.
The value of q++ is q
The value of ++q is q+1
This is a stripped implementation of strcpy function. From this code, we see that pointer p and q are increamented, than dereferenced and q is assigned to p until \0 char has been reached.
It happens the other way around. The value at *p is set to *q, then both pointers are incremented.
When you have int foo = bar++ the increment happens after foo has been set. To have it happen first you would do int foo = ++bar
The while loop's condition is performing post-increment. Equivalently:
while (true) {
char* old_p = p;
const char* old_q = q;
++p; // or p++;
++q; // or q++;
*old_p = *old_q;
if (*old_p == '\0')
break;
}

Resources