NOGDUS $1670.00 has been donated to NOGDUS!
September 21, 2017, 01:58:30 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: C++ - Obscure Syntax  (Read 4980 times)
0 Members and 1 Guest are viewing this topic.
Richard Marks
Administrator
Offline Offline

Respect: 3425
« on: March 01, 2010, 03:57:35 PM »

OK, today I just discovered an apparently undocumented (as far as I can tell - hours of googling has returned nothing) feature of the C++ language.

You likely already know that you can create arrays of data types using the brackets:
Code:
int myarray[100];
And that the size is optional if you initialize inline:
Code:
int myarray[] = {1,2,3,4,5,6};
And that you access the elements of the array like this:
Code:
int index = 4;
int value = myarray[index]; // value = 5 using the array init above

But here is what I just learned.

You can use the array name as the index and the index as the variable! Shocked

Code:
int index = 3;
int value = index[myarray]; // value = 4 using the array init above

This also works:
Code:
int value = 3[myarray];

Woah.. coolness!
This lets you do some interesting things.
Here is a fun little program.
See if you can figure out what it does BEFORE you try it out!
Code:
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
    char god[] = " !ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int am_a[] = { 9, 6, 13, 13, 16, 0, 24, 16, 19, 13, 5, 1 };
    for (unsigned int i = 0; i < (sizeof(am_a) / sizeof(int)); i++)
    {
        cout << i[am_a][god];
    }
    cout << endl;
    return(0);
}

Pretty slick stuff. I love C++. Cool
Logged

kingthomas
Offline Offline

Respect: 10
« Reply #1 on: March 01, 2010, 05:22:18 PM »

Awesome, I never knew about 3[myArray]. Good stuff to know.
Logged
xhunter
Guest
« Reply #2 on: March 02, 2010, 04:36:09 AM »

Amazing and rare stuff ! hope to find something like this in Ruby lol !
Logged
Richard Marks
Administrator
Offline Offline

Respect: 3425
« Reply #3 on: March 02, 2010, 04:43:01 AM »

Amazing and rare stuff ! hope to find something like this in Ruby lol !

I can't fathom why you would want to find something like that in Ruby Roll Eyes
But here is my favorite Ruby reference site. Cool
Logged

RedSlash
Offline Offline

Respect: 10
« Reply #4 on: March 04, 2010, 10:20:53 PM »

This topic should be "C++ obscure syntax" rather than "c++ advanced arrays". Great find!
Logged
Richard Marks
Administrator
Offline Offline

Respect: 3425
« Reply #5 on: March 05, 2010, 12:45:30 AM »

Done. Cool
Logged

xhunter
Guest
« Reply #6 on: March 13, 2010, 09:52:45 AM »

Useful link Richard, I'll bookmark it, but you could also use the documentation a reference.
Logged
Richard Marks
Administrator
Offline Offline

Respect: 3425
« Reply #7 on: March 13, 2010, 02:16:44 PM »

Useful link Richard, I'll bookmark it, but you could also use the documentation a reference.

The documentation is the #1 reference; but a single page quick reference is very useful when you know what you are looking for, but don't remember the exact syntax. Cheesy
Logged

Tags: c++ code  arrays advanced arrays 
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
.: Theme by Richard Marks :.
Valid XHTML 1.0! Valid CSS!