This question already has answers here:
Post-increment and Pre-increment concept?
(14 answers)
Closed 7 years ago.
just today I stumbled across this short piece of code that confused me a little.
#include <iostream>
#include <iterator>
int main()
{
int array[] = {0,1,2,3,4,5,6,7,8,9};
auto start = std::begin(array);
while (start != std::end(array))
std::cout << *start++ << std::endl;
}
The thing that is confusing me here is that the 0 is the first output. I read a lot of posts regarding the order of the 2 operators and every single one said: "start" would be incremented first, THEN dereferenced. But std::begin() returns an iterator to the beginning of the array. With this being said, if I increment the pointer to the beginning of the array first before dereferencing it, shouldn't my first output be the 1?
Thanks in advance!
*start++ uses the post increment operator. With post increment the item is incremented but the value returned is the value before incrementing. You are dereferencing that value so that is why the output starts at 0.
Related
This question already has answers here:
Why can a C++ iterator be dereferenced although it isn't a pointer?
(3 answers)
Closed 3 years ago.
I have taken this exaple from cplusplus.com. I have this code :
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
std::cout << *it;
std::cout << '\n';
return 0;
}
What purpose does the asterisk after "cout<<" have? If i remove that asterisk it gives me error C2679 in VC++, which commonly apears when you forget to include <string>, but in my case i have included <string>.
The asterisk in this case means "dereferencing". That is; don't print the iterator, print what the iterator points to (just like you'd dereference a pointer to print what it points to rather than just printing the value of the pointer).
This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 7 years ago.
I saw the following line of code in C++. I have trouble understanding it. I hope that I can get some help here.
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a[] = {1,2,3,4};
cout<<*a+1<<;
cout<<a[1];
}
Question:
I don't understand how *a+1 works. It seems pretty unintuitive - are we adding 1 to the array here?
To understand the expression *a+1 you should consider the operator priority:
the dereference * has higher priority, so a (the name of an array is also the pointer to the first element) is dereferenced, giving the value 1
Then, 1 will be added to the value.
operator priorities are describe here http://en.cppreference.com/w/cpp/language/operator_precedence
In the expression *a + 1, the array a is evaluated in pointer context, so it points to the first value in the array.
Next, *a dereferences that pointer (meaning it gets the value the pointer points to), and that gives you the first element in the array which is 1.
Finally, 1 is added to that value and printed, so it outputs 2.
First of all this statement
cout<<*a+1<<;
^^^^
is syntactically wrong.
I think you mean something like
cout << *a+1 << endl;
Expression *a is equivalent to a[0]. So you could write instead
cout << a[0] + 1 << endl;
Take into account that semantically a[0] + 1 is not the same as a[1] though applied to your example the both expressions will yield the same result.:)
This question already has answers here:
Why does i[arr] work as well as arr[i] in C with larger data types?
(5 answers)
Closed 7 years ago.
The following code
#include<stdio.h>
int main()
{
int arr[] = {10,20,30};
cout << -2[arr];
return 0;
}
prints -30. How? Why?
In your case,
cout<<-2[arr];
gets translated as
cout<<-(arr[2]);
because,
array indexing boils down to pointer arithmatic, so, the position of array name and the index value can be interchanged in notation.
The linked answer is in C, but valid for C++ also.
regarding the explicit ()s, you can check about the operator precedence here.
Look at this statement
cout << -2[arr];
First, know that even though it looks strange the following is true
2[arr] == arr[2]
That being said operator[] has higher precedence than -. So you are actually trying to invoke
-(arr[2])
In C and C++, 2[arr] is actually the same thing as arr[2].
Due to operator precedence, -2[arr] as parsed as -(2[arr]). This means that the entire expression evaluates to negating the 3rd element of arr, i.e. -30.
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
Example code
int arr[3] = { 0, 1 };
int* buf = arr;
*buf++ = *buf + 10;
Result of last expression is that buf[0] == 10. I taught it would be buf[0] == 11.
A college of mine wrote something similar to the example code and I taught it works differently than it does. And I would like to know why it works the way it does.
The way I went about figuring it out was to look at the operator precedence table. There it states that suffix ++ has precedence over dereference. Hence I taught that on the left of operator= buf would point to the first element, but on the right of the operator= it would have already been incremented and would point to the second element. However that is not the case.
My question is, why is that so? Preferably a standard quote :) However any explanation is welcome!
You are accessing and modifying the pointer multiple times in a single sequence point. This is undefined behavior.
More generally, reading from and writing to any variable between sequence points is undefined. The fact that you have a pointer in this specific example is by-the-by.
To avoid confusion with the pointer:
int i = 0;
i++ = i + 1; // UB
Logically, should the i on the right hand side be the "current" value of i, or the modified value? This is why it is undefined. Instead, separate the code:
int i = 0;
++i;
i = i + 1;
Which is clear, and well defined.
This question already has answers here:
pointer increment and dereference (lvalue required error)
(4 answers)
Closed 9 years ago.
int main()
{
int[] x={1,2,3,4,5};
printf("%d",x);
printf("%d",*x);
printf("%d",++*x);
printf("%d",*x++); //Here Lvalue required erroe is genrerated
}
Can someone please explain me what is meaning of this error and why it is generating here
You can't increment an array.
*x++ is the same as *(x++). Maybe you wanted (*x)++ instead?
The correct way to declare array in c/c++ is :
int x[]={1,2,3,4,5};
The error is because you can not use ++ on array
In this statement:
printf("%d",*x++);
You are applying the ++ operator to the array itself, which is not possible. This gives you the lValue error.
Alternatively, in this one:
printf("%d",++*x);
You are applying the ++ operator to the value pointed to by the array (its first element), which of course works fine and just prints the value of next element.
Try
printf("%d",(*x)++);
You can't increment an array. Try this:
int main()
{
int x[]={1,2,3,4,5};
printf("%d",x);
printf("%d",*x);
printf("%d",++*x);
int *y=x;
printf("%d",*y++); //you can increment a pointer though
}
its ok the increment in array makes no sense.But array is just a pointer variable. I mean if I print the value of x in my program(which is an array) it will print address of a[0].It mean it is pointer to a[0].So when I can use increment on a simple pointer variable then why I can not use increment operation on array pointer.