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 !~
The compiler doesn't do
a + 5
, it is worse than that - it doesa + 5 * sizeof(5)
, so5[a]
becomes5 + a * sizeof(a)
, which is OK, sincea
is a pointer, and pointers are integers that can be added and multiplied. Learning something new about Haskell is like buying a new clothes. Learning something new about C/C++ is like discovering the nuclear reaction - it can change you and not always for good. Please, please, don't get twisted, don't go into C/C++ land! :) -
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 !~
Thanks, This was the question I used on applicants who claimed (on a scale of 1..10) a level 9 or 10 knowledge of the C language. Also, I believe a[5] == *(a+5) Because a[5] gives you the VALUE, not the pointer to the value The rule as I learned it, is every [] => * level of de-referencing.
-
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.
Well, learning C at this era is also upto no good. But, it is worth a shot. :-) Using your example, I would be much more interested in the "man inside" rather than the bad cover-up.
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:
there are many frameworks and systems that a programmer must know
The average programmer only needs to know a few (and knows less) :) It's good to know a bit about everything because every new language or framework gives you a new perspective on the ones you already knew. For example, knowing Haskell made me really appreciate C# again! :laugh:
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
Actually its been awhile since I have programmed in C# or .NET framework. I have been on a tour for C/C++, cross-platform Java, Haskell and Shell programming. IDEs like, Qt, Eclipse (on Linux) and Text editors to write code (without any syntax highlighting) to be compiled using command line compilers and so on. No doubt, Microsoft has provided us with great tools and languages for programming. Going into native-ness is sometimes pain. ;)
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
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!
-
Actually its been awhile since I have programmed in C# or .NET framework. I have been on a tour for C/C++, cross-platform Java, Haskell and Shell programming. IDEs like, Qt, Eclipse (on Linux) and Text editors to write code (without any syntax highlighting) to be compiled using command line compilers and so on. No doubt, Microsoft has provided us with great tools and languages for programming. Going into native-ness is sometimes pain. ;)
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
You masochist, you! :laugh:
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
-
You masochist, you! :laugh:
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:
You masochist, you! Laugh | :laugh:
I'll take that as a compliment. :rose: ;)
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 !~
-
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 !~
For the record, this behavior is part of C since K&R. It exists in C++ for backward compatibility. I am not an objective-C guy, but suspect it has same behavior.
-
"C allows you to shoot yourself in the foot. C++ makes it harder, but if you succeed, it will take off your whole leg!" - Unknown
Fletcher Glenn
fglenn wrote:
Unknown
It actually is known ... Bjarne Stroustrup: http://www.stroustrup.com/bs_faq.html#really-say-that[^]
-
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've had to use this knowledge before for an interview. They wanted me to explain how an array could be a constant lookup, and how they worked. This example is extremely good for just being able to understand how an array stores memory and how is accesses it.