Download presentation
Presentation is loading. Please wait.
1
Pointers Lecture 2 Tue, Jan 24, 2006
2
Topics Big Endian vs. Little Endian Pointer Arithmetic
Array Names as Pointers Pointers as Array Names Pointers as Function Parameters Arrays as Function Parameters Function Parameters in C
3
Big Endian and Little Endian
Suppose we make the assignment int i = 0x ; If the architecture is Big Endian, then it is stored as If the architecture is Little Endian, then it is stored as 76 54 32 10 10 32 54 76
4
Example: Endianness
5
Pointer + int It is permissible to add a pointer and an integer. p + i
The integer is multiplied by the size of the object pointed to That value is added to the address. The result is a pointer of the same type.
6
Pointer + int The integer is interpreted as the number of objects of that type between the original address and the new address. The resulting address is interpreted as a pointer to the object located a specified number of positions to the right, as in an array.
7
Pointer Arithmetic char* pc = &c; int* pi = &i; double* pd = &d;
char* pc2 = pc + 3; // Adds 3 int* pi2 = pi + 3; // Adds 12 double* pd2 = pd + 3; // Adds 24
8
Pointer Arithmetic int i; int* p = &i; char* q = (char*)p; q p
9
Pointer Arithmetic int i; int* p = &i; char* q = (char*)p; q q+4 p p+1
10
Pointer Arithmetic int i; int* p = &i; char* q = (char*)p; p p+1 p+2 q
11
Example: PtrPlusPtr.cpp
12
Pointer Minus int This is the same as addition, but the integer is subtracted. p – i The result is a pointer. The result is interpreted as a pointer to the object located a specified number of positions to the left, as in an array.
13
Pointer Minus Pointer Subtraction of one pointer from another pointer (of the same type) is permitted. Take the difference of the addresses. Divide it by the size of the object pointed to. The result is an int. Interpret the result as the number of objects of that type in between two addresses.
14
Pointer Arithmetic p q
15
Pointer Arithmetic q – p = 5 p q
16
Example: PtrMinusPtr.cpp
17
Examples: Pointer Arithmetic
Legal operations int diff1 = (p2 – p1) + 4; int diff2 = p2 – (p1 – 4); int diff3 = p2 – p1 + 4; int diff4 = p2 + 4 – p1; Are they equivalent?
18
Example: Pointer Arithmetic
Pointer addition is illegal int* mid = (p1 + p2)/2; // Illegal Legal int* mid = p1 + (p2 – p1)/2;
19
An Array Name as a Pointer
An array name points to the first member of the array. The value of the array name cannot be changed, i.e., it is a constant pointer. An array name may be assigned to a pointer of the same type. int a[3] = {10, 20, 30}; int* pi = a;
20
A Pointer as an Array Name
A pointer name may be indexed. p[i] is equivalent to *(p + i). int a[3] = {10, 20, 30}; int* pi = a; cout << pi[1] << endl; cout << *(pi + 1) << endl;
21
Example: PointerLoop
22
Pointers as Function Parameters
A pointer may be used as a function parameter. The pointer is passed rather than the object that it points to. This is similar to passing by reference. To access the object within the function, the pointer parameter must be dereferenced.
23
Example: Pointer as a Function Parameter
int LetterCount(string* str) { int count = 0; for (int i = 0; i < (*str).size(); i++) if (isalpha((*str)[i])) count++; return count; }
24
Example: LetterCount.cpp
25
Arrays as Function Parameters
When an array is “passed” as a parameter, the address of the first element of the array is passed rather than the entire array. It may be treated as a pointer in the function.
26
Example: Array as a Function Parameter
void Sort(int list[], int size) { for (int i = 0; i < size – 1; i++) int* q = list; while (q < list + size - 1) if (*q > *(q + 1)) Swap(q, q + 1); q++; } return;
27
Example: Array as a Function Parameter
void Swap(int* p1, int* p2) { int temp = *p1; *p2 = *p1; *p1 = temp; return; }
28
Function Parameters in C
C did not allow reference parameters. Pointers were passed if the function was to modify the parameter. For efficiency, pointers were passed if the parameter was larger than 4 bytes.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.