This question already has answers here:
Accessing arrays by index[array] in C and C++
(2 answers)
Closed 9 years ago.
Today when i was coding inside my visual studio i unintentionally did following
for(int i=0;i<10;i++)
{
cout<<"Value is"<<[i]arr<<endl;
}
instead of arr[i] and it worked.why it worked?
Because [i]arr == *(i + arr) == arr[i]
Note: + operator holds commutative property
Related
This question already has answers here:
C++ array[index] vs index[array] [duplicate]
(4 answers)
is int[pointer-to-array] in the C++ - standard? [duplicate]
(3 answers)
Closed 2 years ago.
int a[5]={1,2,3,4,5};
int*p=a;
cout<<4[p];
return 0;
can anyone please tell me how it is giving output 5. i know it is 5 because it is the 4th element in the array. but why is 4[p] giving the output!
This question already has answers here:
Is subtracting larger unsigned value from smaller in C++ undefined behaviour?
(2 answers)
Is unsigned integer subtraction defined behavior?
(6 answers)
Closed 3 years ago.
vector<int> a(10);
vector<int> b(20);
if ((a.size() - b.size()) > 1) {
cout << "yes";
}
Can anyone explain why this code prints "yes". If I try storing a.size() - b.size() in an int variable, it is working as expected.
This question already has answers here:
integer indexed with a string in c++ [duplicate]
(3 answers)
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 4 years ago.
Consider the below syntax :
#include <iostream>
int main() {
std::cout << 1["ABC"] << std::endl;
}
The program outputs B which leads me to believe that it behaves as "ABC"[1]. Is that possible, what is the reason behind this ?
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Which of the following combinations of post & pre-increment operators have undefined behaviour in C?
(7 answers)
Closed 6 years ago.
Hello Stackoverflowers,
Considering the following pre-increment addition, can you explain me why j = 8 in the following code ?
int i = 2;
int j = ++i + ++i;
//j = 8, why ??
This question already has answers here:
Returning a reference to a local or temporary variable [duplicate]
(5 answers)
Can a local variable's memory be accessed outside its scope?
(20 answers)
Return address of local variable in C
(5 answers)
Closed 9 years ago.
I saw some weird behavior with my C++ function whose work is to simply put 'n' ones(1).
char *getSpaces(int n) {
char s[50];
int i = 0;
for(i=0; i<n; i++) {
s[i] = '1';
}
s[i] = 0;
return s;
}
When I do fout<< getSpaces(20), I get following output in the file :-
1111111111SOME_WEIRD_CHARACTERS_HERE
Can anybody explain this?
P.S. I am using codeblocks IDE on windows platform.