http://www.youtube.com/watch?v=6pmWojisM_E
CS-1030 Dr. Mark L. Hornick 1
Pointers are
a data type
Since they hold memory addresses, all pointers are the same size in bytes (unlike the data they point to)
int *pValue; // pointer to an integer int* pValue; // another way of declaring the same
The exception is the NULL pointer, where int* pValue = NULL; // points to address 0 // or int* pValue = 0;
This is a convention that allows you to check to see if a pointer is valid
Since a pointer holds an address, the pointer can be made to point anywhere in (data) memory
uint8_t* pValue; // 16-bit addr of 8-bit value pValue = 0x0038; // what is at 0x38???
char s[]=ABCD;
The string variable is actually a pointer to the beginning of the array, so we can write:
char* ps = s; // or ps = &s[0];
Dereferencing the pointer gives the value of the character at that address:
Incrementing the pointer advances the pointer address to the next character in the array:
10
Pointer Arithmetic
Incrementing a character pointer advances the pointer address by one byte, to the next character in the array:
Incrementing a long pointer advances the pointer address by four bytes, to the next long in the array:
pl++; // value of pl increases by 4; Pointer arithmetic is dependent on the size of the element the pointer is declared to reference.
11
uint8_t* means pointer to unsigned 8-bit int uint16_t* means pointer to unsigned 16-bit int Since Atmega32 uses 2-byte addressing, all pointers are 2 byte variables
px(high) px(low)
Pxs value is the address of some 8-bit uint8_t value somewhere in memory
Calling such a function: uint8_t x=3; uint16_t y=4; uint8_t* px = &x; uint16_t* py = &y; int16_t sum = add(&x, &y); sum = add(px, py);
CE-2810 Dr. Mark L. Hornick
12
Within the function, the arguments must be dereferenced to manipulate the values
Implementing such a function: uint16_t add(uint8_t* px,uint16_t* py ) { uint8_t tempx = *px; uint16_t tempy = *py; uint16_t sum = tempx + tempy; // or uint16_t sum return sum; } = *px + *py;
13
14