Pointer To Function
Pointer To Function
Pointer To Function
Pointers
myDatao
3000
myData1
4000
myData2
4200
myDatan
5000
int j = 3;
3000
3010
3020
pa
3000
3010
3020
pa
3000
0003
pa
3000
0004
/*
* A First Look at Pointers
*
* Playing with pointers.
*/
#include <stdio.h>
int main()
{
int x=1, y=2, z;
int *pa;
/* pa is a pointer to int */
pa = &x;
/* pa now points to x */
z = *pa;
printf (The value of z is: %i\n, *pa);
/* z is now 1 */
*pa = y;
printf (The value of x is: %i\n, *pa);
/* x is now 2 */
(*pa)++;
printf (The value of x is: %i\n, *pa);
/* x is now 3 */
return 0;
}
Let
intPtr point to memory address 3000
floatPtr point to memory address 4000
intPtr + 1
gives 3010
or
intPtr++
gives 3010
*pa = 0003;
*(pa++) = 0010;
*(pa + 1) = 0010;
*pa + 1 = ?
pa
3000
Pointer Arithmetic
Cannot
Add Pointers
Multiply Pointers
Divide Pointers
Multiply by Scalar
Divide by Scalar
Can
Subtract Pointers
Add Scalar
Know
First Address
Last Address
(highAddress - lowAddress) / 2
10
Pointer Comparison
Legal Comparisons:
==, !=
Determine if the two pointers do or
do not point to the same address
11
Arrays
An array is a group of consecutive memory
locations that are organized to hold a collection of values of a single type.
syntax
type identifier [ dimension ]
12
type -
identifier -
dimension -
The declaration:
int a[10];
Specifies:
13
Array Representation
Array Elements
Numbered beginning at 0
Schematically
a[0] a[1] a[2]
a[n]
a[9]
14
3000 - 300F
3010 - 301F
3020 - 302F
.
.
.
3090 - 309F
Compute
starting address + 5 * sizeof an int
3000 + 5 * 16 = 3050
15
The declaration
int j = a[5];
The statement
a[7] = i;
16
Initializing Arrays
Declare a 3 element array
int a[3];
The declaration
Allocates 6 bytes of storage.
Does not set the elements to any value.
17
Case 1:
for (i=0; i<MAXSIZE; i++)
{
a[i] = 0;
}
Case 2:
int a[] = { 4, 5, 6 };
18
a[n]
Stored in memory as
3000
3010
a[0]
a[1]
3020
3030
a[2]
a[3]
.
.
.
.
.
.
30n0
a[n]
19
Thus:
&myArray[0]
myArray
To point to an array:
int myArray[10]; /* declare an array */
int* myArrayPtr; /* declare an array pointer */
myArrayPtr = myArray;
20
/*
* Pointers and Arrays.
*
* Pointing to an array and to its contents
*/
#include <stdio.h>
int main()
{
int a[] = {1,2,3,4,5};
int *pa = a;
int i;
/* pa is a pointer to int */
return 0;
}
21