Data Types
Integer
char unsigned char
short unsigned short
int unsigned int
long unsigned long
enum
Floating
float
double
long double
structured
address
pointer
reference
Pointers
Pointer Variables
int beta;
int* intPtr = β // or int *intPtr;
beta = 28; // direct addressing
*intPtr = 28; // indirect addressing
note:
int* a, b; // int* a;
// int b;
TimeType startTime(8, 30, 0);
TimeType* timePtr = & startTime;
(*timePtr).Increment();
timePtr->Increment();
Pointer Expressions
Pointer operators
- address operator : &
- indirection or deferencing operator: *
Example: Using the & and * operators
Null Pointer
intPtr = 0; // points to absolutely nothing
intPtr = NULL; // #include <stddef.h>
Pointers and Arrays
int A[] = {2, 4, 6, 8, 10};
int * B; // B is an integer pointer;
B = A; // but neither A = B;
// nor B were another array
cout << B[0]; // prints "2"
B[0] = 0; // Sets B[0] to 0
B[1] = 17;
cout << A[1]; // prints "17"
void ZeroOut( float [], int);
int main()
{
float score[45];
ZeroOut( score, 45 );
}
void ZeroOut( float A[], int size)
{
int i;
for (i=0; i<size; i++)
A[i]=0.0;
}
void ZeroOut( float* A, int size)
{
... // same as above
}
Example:
- Using subscripting and pointer notations with arrays
- Card shuffling and dealing
Function Pointers
A pointer to a function contains the address of the function in memory.
Pointers to functions can be passed to functions, returned from functions, stored in arrays,
and assigned to other funtion pointers.
void bubble( int [], const int, int (*) ( int, int ) ); // function prototype
void bubble( int work[], const int size,
int (*compare) ( int, int ) ) // function definition
{
...............
};
Example: Multipurpose sorting using function pointers