Arrays: Memory


Arrays and Pointers

If you are not already familiar with pointers, you should review the Spark Note on memory and pointers.

All of this time we have been using our array variable with the syntax [<index>] to index into the array. We, will now discuss the actual value stored in the array variable. You may have guessed that an array variable holds an entire array the same way that an integer value holds an entire integer. This is not the case. Instead, the array value itself is just a pointer to the memory address where the array begins, as illustrated in the following image:

Figure %: Pointer to Array

This is the reason that you cannot simply assign the value of one array into another array and expect it to create a new copy. Instead, if you have two array variables, and you assign one to the other it will simple mean that you are assigning the address where the first array starts into the second array so that they both point to the same chunk of memory.

Figure %: Array Assignment

If you were planning to use a variable to store only an address, then you would not necessarily want to allocate a chunk of memory when you declared it. To achieve such a variable you could either use the syntax for declaring a pointer:


int *arr_p;

Or you could declare it as you would a normal array but just leave the square brackets empty.


int arr_p[];

In the next section we will discuss more of the implications of arrays being pointers.

Take a Study Break

SparkLife

Star Trek gets SEXY

Chris Pine and Zoe Saldana heat up the red carpet!

SparkLife

Are you afraid of relationships?

Auntie SparkNotes can help!

SparkLife

Wanna get JLaw's gorgeous glow?

Click here for simple, sexy makeup tricks!

SparkLife

Sexy starlet style

See every single look from the Met Gala!

SparkLife

Who'd be on your zombie-apocalypse crew?

We already dib'sed Genghis Khan.

Geek out!

The MindHut

Geeky Actors: Then and Now

Travel back in time!

The MindHut

10 Movies Better Than Their Books

What do you think?

The MindHut

How To Look Like J-Law...

When you don't look like J-Law.

The MindHut

12 Scientific Inaccuracies in Into Darkness

What did Star Trek get wrong?

The MindHut

Villains We Want These Actresses to Play

From super cute to super bad!

The Book

Cover image

Read What You Love, Anywhere You Like

Get Our FREE NOOK Reading Apps