Well I haven't Posted anything vaguely Informative in a long time....Actually in ever?
So i will post Something i was fiddling with for an hour.
Most everyone who uses C++ knows that
arr[0] equates to *(arr+0), but i don't think many people think past this to multi level pointers. like arr[4][9][6].
So i wrote a program/Brainstormed and fiddled to figure out how this works. Well i'm not the greatest with words so i will just show you the source code i eventually finished with.
[php]
#include <iostream>
using namespace std;
template<typename T>
void displayArray(T a[], int i);
// arr[5] == *(arr+5);
// arr[5][3] == *( (*(arr+5)) + 3);
// arr[5][3][2] == *( *( *(arr+5) + 3) + 2)
int main()
{
char* szpString = "This is a char*, kind of array-ish"; // Kinda the same as char szString[] = "This is an array kind of";
char szaString[] = "This is an explicitly defined array";
//The above is basically the same thing.
cout << szpString << endl << szaString << endl << endl;
int numArr[6] = {8, 6, 4, 3, 2, 1};
displayArray(numArr, 6);
//Ok above is a normal array of ints, and a function that display this array
// numArr[6] is equivalent to *(numArr+6), which most everyone knows,
// So i don't think it is necessary to write code for this part
//Now here is where it gets interesting
int MultiArray[2][2] = {
{0, 101},
{253, 325}
};
//Above here is a multilevel array, i was curious about how it broke down
cout << endl << *( *(MultiArray+0) + 1) << endl << *( MultiArray[0] +1 ) << endl << MultiArray[0][1] << endl << endl; // These all mean the same thing!
//After some fiddling i finally made sense of it =), That's how multilevel arrays work apparently
int** pointArr = new int*;
*pointArr = new int;
**pointArr = 1337;
cout << **pointArr << endl << *pointArr[0] << endl << pointArr[0][0];
// These are all equivalent =)
//I tied it back to pointers to pointers by accessing the int** with the [] operator
//Blowing my brain is fun.
//It seems that multilevel arrays are pointers to pointers,
//If you only use one [] operator when you should be using two
//You get an address, so from there i thought of how normal arrays worked
//and ended up with this above.
cin.get();
return 0;
}
template<typename T>
void displayArray(T a[], int i)
{
for (int q = 0; q < i ; q++)
{
cout << a[q] << ", ";
}
cout << endl;
}
[/php]
Because I basically wrote this as it came to my head, there is no particular order, nor does there need to be, because i wrote this specifically to understand how a part of the C++ language works. And the Output is pretty unimpressive also. And everything is quite sporadicly done, and probably explained awkwardly/incorrectly. And really there is little actual worth to this. Either way if there are corrections to be made in the things i said, correct me, i really don't feel like thinking at the moment.
So...Um....yay icecream?