CSc 352: Lecture-8

Introduction to Arrays, Pointers

Note: All the programs mentioned in this lecture are in:

/home/cs352/SUMMER02/lecture8progs/


- ONE DIMENSIONAL ARRAYS 

	- Declaration:
	int numbers[10];  declares an array of 10 integers. 

	In general: 

	elem_type array_name[integral_expr]

	where integral_expr specifies the number of elements. 
	If the integral_expr   evaluates to size, then the 
	array subscripts range from 0 to size-1

	Can be of storage class automatic, external or static, 
	but not register.


	- Initialization:
	int a[]={1 ,2 ,3, 4};     is equivalent to 
	int a[4]={1, 2, 3, 4}

	int a[100]={0} initializes all the elems to 0.
	(If the list is shorter than the array size, 
	the rest of the elems are initialized to 0.)

	If external or static array, and no initialization
	then all elems automatically initialized to 0. 
	However for automatic arrays No such guarantee. 
	See arrayinit.c.


	- Subscripting:
	By a[i], ith elem of array a is accessed. If 
	i is out of bounds, then the value of some
	unrelated variable will be accessed. see bound.c	




- POINTERS

	- If i is a variable, &i is the location(address) 
	of it in memory of its stored value. 
	A Pointer holds an address of a variable.


	- Declaration:
	elem_type *pointer;

	For example:
	int *p;

	p is a pointer to an integer. Its legal range of 
	values start from 0(NULL) and contains a set of positive
	integers which are considered as machine addresses. 


	- Assignments:
	p=0; p=NULL; p=&i; p=(int *)1000;


	- Indirection:
	If p==&i, then *p gives the value of the variable
	that is at the memory location pointed to by p, 
	in this case the value of i. 
	see indirection.c, moreindirection.c


	- Function Calls
	All function calls are call-by-value. In order
	to get the effect of "call -by-reference", so
	that the function can change the values of 
	input parameters inside, use pointers.
	see swap.c




- ARRAYS AND POINTERS

	- Both are addresses.


	- Both can be subscripted.


	- Can think of it as: An array is a pointer
	that contains a fixed address.


	- a[i] is the same as *(a+i) if a is an array.
	Similarly,
	p[i] is the same as *(p+i) if p a pointer.


	- The expression a+i or p+i is pointer arithmetic
	The difference with normal integer arithmetic????
	Gives the address of the ith element.
	see parithmetic.c	


	- Array declaration 
	int a[ARRAY_SIZE];
	allocates contiguous space in memory. 
	Base address is the address of a[0]. 


	- The meaning of 
	int *p;  
	p = a;    /*equivalent to  p = &a[0];  */
	p = a+1;  /*               p = &a[1];  */
	see arraysum.c


	- Illegal Expresssions(Why illegal???):
	&100   /*100 is a constant*/  
	&a     /*a is an array*/
	&(i+5) /*i is a variable*/
	&r     /*r is a register*/
	a = p  /*a is an array, p is a pointer*/
	++a


	- Arrays, Pointers As Function Arguments
	A formal parameter declared as an array
	is actually a pointer.(When an array is passed
	as an argumnet, the base address of the array
	is passed to the function)
	NOTE: The array elems themselves are not copied.	    
	see sumfunc.c


	- see bubblesort.c,  mergesort.c




-STRINGS

        - One dimensional arrays of type char.

        - Ends with '\0', null character.(Included in size)

        - String constants are in double quotes, "abcde"

        - A string consant in fact is a pointer, that
        has as value the base address of the string.
        see strconst.c

        see wordcount.c