wpe41.gif (23084 bytes)CIS3355: Business Data Structures
Fall, 2008
 

How do we use offsets to calculate the address of any dimensional array?

In order to determine how to use offsets, we must understand what arrays are, what their purpose is and how they work. 

Array- a fixed number of contiguous storage elements all of the same data type.

For example: 

      

Now we want to store these elements

First we must declare the array.  The computer needs to know what kind of information it is dealing with.  For example, you cannot tell someone "9" without telling them what context it is in.  We can tell them "9 of spades" and the person knows you are talking about cards.  The computer is the same way.  You cannot give the computer information without telling what type of information it is.  We must tell it if the information we want it to deal with is a character (char = 1 byte), a short (short = 2 bytes) an integer (int = 4 bytes), a float (float = 4 bytes), etc.  The computer must know this information so it knows how to store it. 

Lets declare our array as shorts.

short spades[6];

This is telling the compiler that we want 12 contiguous bytes of storage.  We have 6 elements, and since we declared these elements as shorts (which require 2 bytes (16 bits)) we need a total of 6 * 2 bytes = 12 bytes.  We will name the base address 'spades'.

if we want, we can initialize each element:

short spades[6] = {90,101,112,123,134,145};

The subscripts are the offsets which allow us to be able to determine the address of an element within the array.  We can also use the offset to determine the value stored at that location. 

For example, if we wanted to find where the king of spades(13) was stored in the array, we would need the base address of the array.  That can be determined by typing the following code:

printf("%lu", &spades);

Let's say it gave us the address 15047.  (The operating system is assigning the addresses.) This is where the beginning of the array is stored.

Now we can use this base address to determine the address for any element within the array.

We know that the base address is 15047.  We know that each element within the array is 2 bytes long (short = 2 bytes).  So we know that the first element, (offset 0) is at location 15047.  Since arrays must be stored on contiguous bytes, we know that the next element is stored at 15049 (15047 + 2 bytes = 15049) and the next stored at 15051 and so on and so forth.

15047 (offset 0)

15048 (offset 0)

15049 (offset 1)

9

9

10

15050 (offset 1)

15051 (offset 2)

15052 (offset 2)

10

11

11

15053 (offset 3)

15054 (offset 3)

15055 (offset 4)

12

12

13

15056 (offset 4)

15057 (offset 5)

15058 (offset 5)

13

14

14

 

Now, we can draw a table or count on our fingers by two to find the next address but what if we wanted to know how to find the 75th element?  We don't have the fingers and toes, nor the time to do it that way.  So here is where the offset comes in. 

The offset is sort of a subscripted numbering system for the elements in the array.  If you notice above when we initialized the array, each element had a subscripted number.  The first element begins with zero.  This is our base address.  To find the address for the 5th element of the array (the king, or 13), we can use the following equation:

Element address = base address of the array + (offset number *number of bytes)

Element address = 15047 + (the offset number for the 5th element is 4 * number of bytes is 2 since it is  a short)

Element address = 15047 + (4 * 2) = 15055

(Since the offsets of an array begin at zero, the offset of a particular element of the array will = number of the element -1)

If the array had 75 elements and you wanted to find the 75th element,

Element address = 15047 + ((75-1) * 2)

Element address = 15047 +(74 * 2) = 15195

Now, since we know the data type and address of the element is, we can find out what the value of the element stored at that location is. 

Any questions?!?

Try these:

 

 

 1. In the following code, which value holds the offset[7]?

int [10] = {0,3,9,12,15,18,21,24,27,30}

answer

2. In an array consisting of 102 elements, what is the address of the 64th element?  Assume base address is 1400.

answer

3. The subscript of an element within an array is called:

a.  integer            c.  short

b.  offset              d.  address

answer

4. What two things do we need to know before we can calculate the address of an element within an array?

a.  What program are we using?

b.  What data type are we using?

c.  Where does the array end?

d.  Where does the array begin?

e.  How many elements are in the array?

answer

 

Here are some awesome reference sites for more in depth research:

Introduction to arrays

C++ Notes : Arrays

Dev Articles

 

Answers: 

1. 24  (remember that the first element is offset 0}

Back

 

 

 

 

 

 

 

 

 

 

 

 

 

2. Trick question!!!  We need to know what kind of data type we are working with before we can answer this.

Ok, same question, assume int data type.

element address = 1400 + (63 * 4) = 1652

Back

 

 

 

 

 

 

 

 

 

 

 

3. answer is b

Back

 

 

 

 

 

 

 

 

 

 

 

 

 

4.  answer is b and d

Back