Pointer & Recursive Types
Pointer & Recursive Types
Array
[17] [18][19]
[18]
[19]
Array Declarations
Arrays contain a fixed number of variables of
identical type
Array declaration and allocation are separate
operations
Declaration examples:
int[ ] counts;
double[ ] scores;
String[ ] studentNames;
Array Allocation
Arrays are allocated using operator
~Arrays are special in Java
Array Organization
0
counts
Array Subscripts
Arrays can contain any one type of value
(either primitive values or references)
Subscripts are used to access specific array
values
Examples:
counts[0] // first variable in counts
counts[1] // second variable in counts
counts[9] // last variable in counts
counts[10] // error trying to access
// variable outside counts
Expressions as Subscripts
Array subscripts do not have to be constants
Array subscripts do need to be integer
expressions that evaluate to valid subscript
values for the current array allocation
Examples:
counts[i]
counts[2*i]
counts[I/2]
Array Initialization
Arrays can be initialized by giving a list of
their elements
If your list contains n elements the
subscripts will range from 0 to n 1
You do not need to allocate the array
explicitly after it is initialized
Example:
int [] primes =
{2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
Aliases
It is possible to have two different variables refer to
the same array
When this happens these variables are called
aliases
Creating aliases is not a good programming
practice
Example of how it can happen:
int [ ] A, B;
Passing Arrays to
Functions
Functions
-Lisa Sheehy
Function-level
programming
References
An Introduction to Computer Science Using Java (2nd Edition)
by S.N. Kamin, D. Mickunas, E. Reingold
Dick Pountain. Functional Programming Comes of Age. BYTE.
COM (August 1994). Retrieved on August 31, 2006.
POINTER
Contents
1000 1004
1001
1002
1003
1004
POINTER VARIABLES
Declaration:
type * name
Where:
type defines the type of pointer variable
name pointer variable name
POINTER OPERATORS
POINTER OPERATORS
2) * - a unary operator that returns the value of
the variable located at the address that follows.
Ex.
m contains the memory address of the variable
count.
q = *m;
SAMPLE PROGRAM
#include<stdio.h>
main(void)
{
int count, q;
int *m;
count = 100; /* count is assigned to 100 */
m = &count; /*m receives counts address */
q = *m; /*q is assigned counts value indirectly
printf(%d, q); /* prints 100 */
return 0;
}
through m*/
POINTER EXPRESSIONS
Pointer Assignments
Pointer Arithmetic
POINTER ASSIGNMENTS
POINTER ARITHMETIC
1)
2)
Addition
Subtraction
p2--;
p1 = 2002,
p2 = 1998
POINTER ARITHMETIC
In the case of pointers to characters this appears
as normal arithmetic.
All other pointers inc/dec by the length of the
data type they point to.
Ex.
Assume 1-byte characters, 2-byte integers:
-when a char pointer is incremented, its
value increases by 1.
-when an int pointer is incremented, its
value increases by 2.
POINTER ARITHMETIC
char *ch = 3000;
int *i = 3000;
ch
3000
ch+1
3001
ch+2
3002
ch+3
3003
ch+4
3004
ch+5
3005
}i
}
}
i+1
i+2
All pointer arithmetic is done relative to the base type of the pointer
so that the pointer is always pointing to another element of the base type
Ex.
char str[80], *p1;
p1 = str;
POINTERS
Provide the means by which functions can modify
their calling arguments
To support dynamic allocation system
To improved the efficiency of certain routines
To support certain data structures such as linked
list and binary trees.
RECURSION
A method of defining functions in which the function being
defined is applied within its own definition. The term is also used
more generally to describe a process of repeating objects in a selfsimilar way.
Ex.
/* Compute the factorial of a number */
factr(int n) /* recursive */
{ int answer;
if(n==1) return (1);
answer = factr(n-1) *n;
return(answer);
}
Evaluation:
- factr() is called w/ an argument of 1, the function returns 1;
otherwise it returns the product of factr(n-1)*n.
ADVANTAGE OF RECURSIVE
FUNCTION
END
Reference:
Wikipedia.com
Schildt, Herbert. Turbo C/C++: The Complete Reference, 2 nd Ed. McGrawHill, 1992.