Basics Of How Array Traversal Works

7 February, 2009 § Leave a comment

Often times you will see an array position referenced as:

int arrNumbers[5];
arrNumbers[2] = 3;

Did you know this is the same as writing:

(*arrNumbers + 2)

The reason that both of these are equal is due to the fact that when you use the operator[], pointer arithmatic is getting the memory location of the array and incrementing the position of the variable that you want to look at.  When you declare an array of size n, you are allocating a sizeof(type)*n block of memory. The operator[] simply references locations within that block of memory.

Tagged: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Basics Of How Array Traversal Works at JAWS.

meta