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?)
  2. Where does the character array end?
  3. What is the value of int_ary? Of chr_ary?
  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.)
  5. "The name of an array is a pointer to the array." Describe the difference between the expressions chr_ary and *chr_ary. What are their values?
  6. Describe the difference between int_ary+1, int_ary[1] and *(int_ary+1). What are their values?
  7. What is the value of &chr_ary[0]? Of &chr_ary[1]? Of &chr_ary? (CAREFUL!!!)
  8. What is the value of &int_ary[0]? Of &int_ary?

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)
  2.	chr_ary+1	*chr_ary+1	*(chr_ary+1)
  3.	int_ary		*int_ary	*(int_ary+0)
  4.	int_ary+1	*int_ary+1	*(int_ary+1)	int_ary[1]
  5.	*(chr_ary+3)	chr_ary[3]	chr_ary+3
  6.	chr_ary		chr_ary[0]	*chr_ary	&chr_ary[0]
  7.	int_ary		int_ary[0]	*int_ary	&int_ary[0]
  8.	&int_ary[0]	&int_ary	*int_ary+0	int_ary
	(CAREFUL!!!)
  9.	int_ary[2]	*(int_ary+2)	&int_ary[2]	int_ary+2
 10.	int_ary+10	int_ary[10]	*(int_ary+10)