POINTER WORKSHEET - 2

Relating Expressions

This worksheet is intended to show the relationship between array names, subscripts, and pointer arithmetic. It may be helpful (but not required) to use a sheet of graph paper to represent where the variable values are stored (their addresses or storage locations). It is easier to understand the relationship if you can visualize how the information is stored.

SET-UP

void  main()
{  int   int_ary[10]={2,4,6,8};	// array of 10 integers
   char  chr_ary[11];		// array of 11 characters,
				// room for 10 useable characters
				// and the null terminator
   .
   .
   .
}

Assume that the space reserved for the integer array starts at storage location 2000.
Assume that the space reserved for the character array starts at storage location 1600.

PART 1

  1. Where does the integer array end? (Hint: How many bytes for an integer?)
    10 integers X 4 bytes per integer = 40 bytes.
    2000 + 40 = 2020, so the integer array goes up to, but does not include, location 2040. The last integer space in the array is location 2036.
  2. Where does the character array end?
    11 characters X 1 byte per character = 11 bytes. 1600 + 11 = 1611, so the character array goes up to, but does not include, location 1611. The last space in the array is location 1610. (This does not mean that the null terminator is at location 1610. It could be earlier.)
  3. What is the value of int_ary? Of chr_ary?
    int_ary = 2000 and chr_ary = 1600
  4. What is the value of int_ary[0]? Of int_ary[3]? Of int_ary+1? Of int_ary+9?
    (Based on the declaration statement.)
    int_ary[0] = 2, int_ary[3] = 8 Both expressions refer to actual elements in the array.
    int_ary+1 and int_ary+9 do not refer to elements in the array. They are storage locations.
    int_ary+1 is a pointer to the location 1 integer space past the start of the array.
    int_ary+1 = 2004 (location 2000 + 4 bytes).
    int_ary+9 = 2036 (location 2000 + 36 bytes).
  5. "The name of an array is a pointer to the array." Describe the difference between the expressions chr_ary and *chr_ary.
    chr_ary is a constant pointer to the beginning of the array. It is also a pointer to the first element in the array. *chr_ary is a character in the array.
    What are their values? chr_ary = 1600, *chr_ary = 'c'
  6. Describe the difference between int_ary+1, int_ary[1] and *(int_ary+1).
    int_ary+1 is a pointer to the second element of the array. int_ary[1] and *(int_ary+1) both refer to the second integer in the array.
    What are their values? int_ary+1 = 2004, int_ary[1] = *(int_ary+1) = 4
  7. What is the value of &chr_ary[0]? Of &chr_ary[1]? Of &chr_ary?
    &chr_ary[0] = 1600 (the location of the first element in the array)
    &chr_ary[1] = 1601 (the location of the second element in the array)
    &chr_ary is unknown. It does not equal 1600. chr_ary is a constant pointer to location 1600 (it is stored in another location which was not given).
    For PART 2, assume &chr_ary = 1004.
  8. What is the value of &int_ary[0]? Of &int_ary?
    &int_ary[0] = 2000, &int_ary is unknown. int_ary is a constant pointer to location 2000. The location of int_ary was not given. For PART 2, assume &int_ary = 1006.

If you have been writing out the storage locations, you should have something similar to the charts below.

location 1000 1001 1002 1003 1004,1005 1006,1007 1008 1009
value         1600 2000    
name         chr_ary int_ary    
location 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
value c o m p u t e r \0 ??? ???
name * [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

* names used are chr_ary[subscript]

location 2000,2003 2004,2007 2008,2011 2012,2015 2016,2019
value 2 4 6 8 0
name int_ary[0] int_ary[1] int_ary[2] int_ary[3] int_ary[4]
location 2020,2023 2024,2027 2028,2031 2032,2035 2036,2039
value 0 0 0 0 0
name int_ary[5] int_ary[6] int_ary[7] int_ary[8] int_ary[9]

PART 2

Check the answers to PART 1 before continuing. You need to understand #4-8 inorder to do PART 2.
Assume the following line of code has been executed.
strcpy(chr_ary, "computer");
The values in the integer array have not changed. Determine the value of each expression below.

  1.	chr_ary		chr_ary+0	*chr_ary	*(chr_ary+0)
	1600		1600		'c'		'c'
  2.	chr_ary+1	*chr_ary+1	*(chr_ary+1)
	1601		'd'  		'o'
			'c' + 1 = 'd'
  3.	int_ary		*int_ary	*(int_ary+0)
	2000		2		2
  4.	int_ary+1	*int_ary+1	*(int_ary+1)	int_ary[1]
	2004		3		4		4
	(2000 + 1 int space
	= 2000 + 4 bytes)
  5.	*(chr_ary+3)	chr_ary[3]	chr_ary+3
	'p'		'p'		1603
  6.	chr_ary		chr_ary[0]	*chr_ary	&chr_ary[0]
	1600		'c'		'c'		1600
						     (location of the
						     first element)
  7.	int_ary		int_ary[0]	*int_ary	&int_ary[0]
	2000		2		2		2000
  8.	&int_ary[0]	&int_ary	*int_ary+0	int_ary
	2000		1006		2		2000
  9.	int_ary[2]	*(int_ary+2)	&int_ary[2]	int_ary+2
	6		6		2008		2008
 10.	int_ary+10	int_ary[10]	*(int_ary+10)
	2040		garbage		garbage
			(from the next 4 bytes after the array)