Learnt something "new" about C/C++
-
I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is
a[5]
==5[a]
?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:int main()
{
int a[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << i[a] << ", ";
}
std::cout << std::endl;
system("pause");
return 0;
}
// Output: 1, 2, 3, 4, 5,For those who didn't know it (just like me!) the logic is that C (or C++) translates
a[5]
to*(a + 5)
and then gets the data from that location. Which, is similar to having5[a]
that gets translated to*(5 + a)
. Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is
a[5]
==5[a]
?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:int main()
{
int a[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << i[a] << ", ";
}
std::cout << std::endl;
system("pause");
return 0;
}
// Output: 1, 2, 3, 4, 5,For those who didn't know it (just like me!) the logic is that C (or C++) translates
a[5]
to*(a + 5)
and then gets the data from that location. Which, is similar to having5[a]
that gets translated to*(5 + a)
. Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
And now that you know it, please try to forget it. Because if you use that in "real world" code, somebody is going to smack you round the head. Hard. :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is
a[5]
==5[a]
?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:int main()
{
int a[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << i[a] << ", ";
}
std::cout << std::endl;
system("pause");
return 0;
}
// Output: 1, 2, 3, 4, 5,For those who didn't know it (just like me!) the logic is that C (or C++) translates
a[5]
to*(a + 5)
and then gets the data from that location. Which, is similar to having5[a]
that gets translated to*(5 + a)
. Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
I was aware of that once but forgot it until I read the article C++ is fun: tips and tricks[^].
-
I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is
a[5]
==5[a]
?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:int main()
{
int a[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << i[a] << ", ";
}
std::cout << std::endl;
system("pause");
return 0;
}
// Output: 1, 2, 3, 4, 5,For those who didn't know it (just like me!) the logic is that C (or C++) translates
a[5]
to*(a + 5)
and then gets the data from that location. Which, is similar to having5[a]
that gets translated to*(5 + a)
. Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
I guess it's only for type of the same size as int.... and a good source of endless bug and fun! :-\
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
And now that you know it, please try to forget it. Because if you use that in "real world" code, somebody is going to smack you round the head. Hard. :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Yep, won't make any use of it. But it was interesting fact. :laugh:
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
I was aware of that once but forgot it until I read the article C++ is fun: tips and tricks[^].
Will give it a view, because next task of mine is to forget it as per OriginalGriff's suggestion. ;)
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
I guess it's only for type of the same size as int.... and a good source of endless bug and fun! :-\
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
Nah, on that link, it was shared that compiler runs a multiplication with
sizeof(a)
itself. :-)The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
And now that you know it, please try to forget it. Because if you use that in "real world" code, somebody is going to smack you round the head. Hard. :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Nah! It's for C++ people. Those people love those kind of shoot in your own foot kind of tricks! Show the world that only real man (and real woman too, of course) dare to C++! :laugh: :rolleyes:
All in one Menu-Ribbon Bar DirectX for WinRT/C# since 2013! Taking over the world since 1371!
-
I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is
a[5]
==5[a]
?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:int main()
{
int a[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << i[a] << ", ";
}
std::cout << std::endl;
system("pause");
return 0;
}
// Output: 1, 2, 3, 4, 5,For those who didn't know it (just like me!) the logic is that C (or C++) translates
a[5]
to*(a + 5)
and then gets the data from that location. Which, is similar to having5[a]
that gets translated to*(5 + a)
. Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
Afzaal Ahmad Zeeshan wrote:
I am going to have an exam of C++ tomorrow
Good luck on the exam!
Afzaal Ahmad Zeeshan wrote:
go back to Haskell once again
Yikes! :omg:
Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.
Simplicity is prerequisite for reliability. — Edsger W. Dijkstra
Regards, Sander
-
And now that you know it, please try to forget it. Because if you use that in "real world" code, somebody is going to smack you round the head. Hard. :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Agreed.. because you can doesn't mean you should.
-
Afzaal Ahmad Zeeshan wrote:
I am going to have an exam of C++ tomorrow
Good luck on the exam!
Afzaal Ahmad Zeeshan wrote:
go back to Haskell once again
Yikes! :omg:
Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.
Simplicity is prerequisite for reliability. — Edsger W. Dijkstra
Regards, Sander
Quote:
Afzaal Ahmad Zeeshan wrote:
go back to Haskell once again
Yikes! :OMG:
Trust me, Haskell is much easy and simple language. Although I started learning Haskell 2 days ago, I think I am enjoying the much simpler and better syntax. After all, you also like PHP[^]. ;-p Don't you Sander? :laugh:
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
And now that you know it, please try to forget it. Because if you use that in "real world" code, somebody is going to smack you round the head. Hard. :laugh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
But we both know once something is seen it cannot be unseen and that it will be done just to amaze and confound others.
New version: WinHeist Version
You didn't fall from the stupid tree you got dragged through the whole dumbass forest. -
Quote:
Afzaal Ahmad Zeeshan wrote:
go back to Haskell once again
Yikes! :OMG:
Trust me, Haskell is much easy and simple language. Although I started learning Haskell 2 days ago, I think I am enjoying the much simpler and better syntax. After all, you also like PHP[^]. ;-p Don't you Sander? :laugh:
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
Afzaal Ahmad Zeeshan wrote:
After all, you also like PHP[^]. ;-P Don't you Sander? :laugh:
I really don't... :sigh: Although I use both in my blogs :) PHP: Web development #4: PHP in the back[^] Haskell: How to Learn and Polyglot vs. Specialist[^] Maths in IT #1: Basic set theory[^] Maths in IT #2: Venn diagrams[^] Yeah, that's quite some Haskell snippets! ;)
Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.
Simplicity is prerequisite for reliability. — Edsger W. Dijkstra
Regards, Sander
-
I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is
a[5]
==5[a]
?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:int main()
{
int a[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << i[a] << ", ";
}
std::cout << std::endl;
system("pause");
return 0;
}
// Output: 1, 2, 3, 4, 5,For those who didn't know it (just like me!) the logic is that C (or C++) translates
a[5]
to*(a + 5)
and then gets the data from that location. Which, is similar to having5[a]
that gets translated to*(5 + a)
. Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
Good luck on the exam ! I find that code example frightening :)
Afzaal Ahmad Zeeshan wrote:
I can say I know nothing of C or C++
Well, you said the nothing you don't know well enough on this post to get my up-vote: that's something.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
-
I am going to have an exam of C++ tomorrow, so I thought why not just open the book, swap pages from one to the last one, then close it and go back to Haskell once again. But while I did, I asked myself "Are arrays actually consecutive in memory?". Along with that, I came to another question, "Why is
a[5]
==5[a]
?" that was something I never knew about before. So, I went searching for the answers and tried it on my own machine too, to make my mind actually believe that I was accessing the ath element of array 5. :laugh:int main()
{
int a[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
std::cout << i[a] << ", ";
}
std::cout << std::endl;
system("pause");
return 0;
}
// Output: 1, 2, 3, 4, 5,For those who didn't know it (just like me!) the logic is that C (or C++) translates
a[5]
to*(a + 5)
and then gets the data from that location. Which, is similar to having5[a]
that gets translated to*(5 + a)
. Mathematically, we know 5 + a == a + 5. Thus, compiler accepted that. I hope, I am not alone who didn't know it yet. :laugh: I can say I know nothing of C or C++. For those who want to read a thread, head over to http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a?rq=1[^].The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
Interesting - actually neat - but it would fail, I think if stretched a bit:
int x=5;
!= x[a]; // I think: my work environments were just trashed
// by the server monkeys so I can't test.But your discovery is truly wondrous!
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
-
Interesting - actually neat - but it would fail, I think if stretched a bit:
int x=5;
!= x[a]; // I think: my work environments were just trashed
// by the server monkeys so I can't test.But your discovery is truly wondrous!
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
Seems to work fine.
#include <iostream>
int foo(int x, int *a)
{
return x[a];
}int main()
{
int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int x = 5;std::cout << arr\[x\] << " " << x\[arr\] << " " << foo(x, arr) << std::endl;
}
Compiles and runs as expected without warning.
-
Seems to work fine.
#include <iostream>
int foo(int x, int *a)
{
return x[a];
}int main()
{
int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int x = 5;std::cout << arr\[x\] << " " << x\[arr\] << " " << foo(x, arr) << std::endl;
}
Compiles and runs as expected without warning.
That's unfortunate - I was hoping that the compiler wouldn't accept it because it makes x appear to be an array. It's not that I don't understand - but one could hope that the compiler would differentiate between the two.
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein
"As far as we know, our computer has never had an undetected error." - Weisert
"If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010
-
Afzaal Ahmad Zeeshan wrote:
After all, you also like PHP[^]. ;-P Don't you Sander? :laugh:
I really don't... :sigh: Although I use both in my blogs :) PHP: Web development #4: PHP in the back[^] Haskell: How to Learn and Polyglot vs. Specialist[^] Maths in IT #1: Basic set theory[^] Maths in IT #2: Venn diagrams[^] Yeah, that's quite some Haskell snippets! ;)
Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.
Simplicity is prerequisite for reliability. — Edsger W. Dijkstra
Regards, Sander
Much respect for you, to bear so much pain, Sander! :laugh: I have just started Haskell, and I am very much loving it. I would give these articles of your a look, you have written a great article for Haskell. You got some more I can pay some attention to? :-)
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
Good luck on the exam ! I find that code example frightening :)
Afzaal Ahmad Zeeshan wrote:
I can say I know nothing of C or C++
Well, you said the nothing you don't know well enough on this post to get my up-vote: that's something.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
Thank you, Bill! But may I ask, it was "frightening" in the sense of?
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
Thank you, Bill! But may I ask, it was "frightening" in the sense of?
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
Afzaal Ahmad Zeeshan wrote:
"frightening" in the sense of?
I'd say it's about the same way as I feel looking in a mirror: that sense that what is on the surface is a very bad cover-up of something strange, and probably up to no good :)
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.