View Single Post
Old Sep 8th, 2006, 5:31 PM   #3
synchronized
Newbie
 
Join Date: Sep 2006
Posts: 5
Rep Power: 0 synchronized is on a distinguished road
I think a quick tutorial is in order. Its not that hard just getting to understand the syntax for a novice can be hard. Here I go.

I will start with a 1 dimensional array. A string will do as I am not feeling too creative tonight.

#include <stdio.h>

int main()
{
    char MyArray[] = "Hello World"; 
    
    //To get to the fist element using the [] notation you would do this
    printf("%c\n", MyArray[0]);

    //That would print n, but what if i wanted to use a pointer to 
    //do the same thing
    printf("%c\n", *(MyArray)); 
    //That will produce the same result as before printing a "H"

    //Now what if i wanted to get to the second character(second index)
    printf("%c\n", MyArray[1]);
    printf("%c\n", *(MyArray + 1));
    //All i did here was add one to the address and C is smart enough to
    //move the pointer along to the start of the next value. It does not matter
    // if the size of each element is one byte or 10 bytes it will always work.
}

So that was one dimensional arrays, but what if you want two dimensions

#include <stdio.h>

int main()
{
    char MyArray[3][3] = {
                                     {'1', '2', '3'}
                                     {'4', '5', '6'}
                                     {'7', '8', '9'}
                                  };
    //First lets look at the address of MyArray
    printf("%p\n", MyArray);
    //Now lets look at the adress of MyArray[0][0]
    printf("%p\n", &MyArray[0][0]);
    //Now lets see what is in MyArray[0]
    printf("%p\n", MyArray[0]);

    //If you run that code what will be printed?

   //They will all print the same memory address as simple a 2D array
   // is just an array of arrays. 

   //Take a look at the values in MyArray

   printf("%c\n", MyArray[0][0]);
   printf("%c\n", *MyArray[0]);
   printf("%c\n", **MyArray);
  
   //This should all print 1 as they all refer to the same location. 

 
}

So this is all the ways to get at the elements in MyArray

MyArray[0][0] = *MyArray[0] = **MyArray
MyArray[0][1] = *(MyArray[0]+1) = *(*MyArray+1)
MyArray[0][2] = *(MyArray[0]+2) = *(*MyArray+2)
MyArray[1][0] = *(MyArray[0]+3) = *(*MyArray+3) = *MyArray[1]
MyArray[1][1] = *(MyArray[0]+4) = *(*MyArray+4) = *(MyArray[1]+1)
MyArray[1][2] = *(MyArray[0]+5) = *(*MyArray+5) = *(MyArray[1]+2)
MyArray[2][0] = *(MyArray[0]+6) = *(*MyArray+6) = *MyArray[2] = *(MyArray[1] + 3)
MyArray[2][1] = *(MyArray[0]+7) = *(*MyArray+7) = *(MyArray[2]+1) = *(MyArray[1] + 4)
MyArray[2][2] = *(MyArray[0]+8) = *(*MyArray+8) = *(MyArray[2]+2) = *(MyArray[1] + 5)

So you can see the pattern and how it works. I will leave it for someone else to make some contrive example for a 2D array.

If i have made a silly error then please feel free to kill me :p

Last edited by synchronized; Sep 8th, 2006 at 5:56 PM.
synchronized is offline   Reply With Quote