1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
-
Upload
james-hulen -
Category
Documents
-
view
223 -
download
0
Transcript of 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
![Page 1: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/1.jpg)
1
Chapter ThirteenChapter Thirteen
PointersPointers
![Page 2: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/2.jpg)
2
PointersPointers
• A pointer is a sign used to point out the direction
![Page 3: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/3.jpg)
3
PointersPointers
• A pointer is a data item whose value is the
address in memory of some other value
12
1000
10001001100210031004100510061007
![Page 4: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/4.jpg)
4
PointersPointers
• Allow you to refer to a large data structure in a compact way
• Facilitate sharing data between different parts of a program
• Make it possible to reserve new memory during program execution
• Can be used to record relationships among data items
![Page 5: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/5.jpg)
5
VariablesVariables
• Each variable refers to some location in memory and therefore has an address
• Once a variable has been declared, the address of the variable never changes, even though the content of the variable may change
• Depending on the type of data they contain, different variables require different amount of memory
![Page 6: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/6.jpg)
6
Lvalue and RvalueLvalue and Rvalue
x = x;
Store the content of the memory location at address 1000to the memory location at address 1000
121000100110021003
x:
Lvalue: address Rvalue: content
![Page 7: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/7.jpg)
7
Lvalue-ExpressionsLvalue-Expressions
• An expression that refers to a memory location capable of storing data has an lvalue
x = 1.0;intarray[2] = 17;
• Many expressions do not have lvalues1.0 = 1.0; /* illegal */x + 1.7 = 17; /* illegal */
![Page 8: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/8.jpg)
8
Lvalue-ExpressionsLvalue-Expressions
• Each lvalue-expression refers to some location in memory and therefore has an address
• Once it has been declared, the address of an lvalue-expression never changes, even though the contents of the lvalue-expression may change
• Depending on the type of data they contain, different lvalue-expressions require different amount of memory
• The address of an lvalue-expression is itself data that can be manipulated and stored in memory
![Page 9: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/9.jpg)
9
Pointer DeclarationsPointer Declarations
• Pointers can be declared asbase-type * pointer-variable;
int *iptr;
char *cptr;int *p1, *p2;int *p1, p2;
![Page 10: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/10.jpg)
10
Pointer OperationsPointer Operations
• & : address-of returns the address of an lvalue-expression int x, *p; p = &x; p = &8; /* Illegal */
• * : value-pointed-to (dereferencing) refers to the memory location pointed to by a pointer int x, *p; p = &x; /* *p x */ x = *p;
![Page 11: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/11.jpg)
11
ExamplesExamples
int x, y;int *p1, *p2;
x = -42;y = 163;
p1 = &x;p2 = &y;
-42 163 1000 1004
1000100410081012
x:y:p1:p2:
![Page 12: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/12.jpg)
12
ExamplesExamples
/* *p1 x, *p2 y */*p1 = 17;
/* *p1 y, *p2 y */p1 = p2;
/* *p1 y, *p2 y */*p1 = *p2;
17 163 1000 1004
1000100410081012
x:y:p1:p2:
17 163 1004 1004
1000100410081012
x:y:p1:p2:
17 163 1004 1004
1000100410081012
x:y:p1:p2:
![Page 13: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/13.jpg)
13
The Special Pointer NULLThe Special Pointer NULL
• In many applications, it is useful to be able to store in a pointer variable a special value indicating that the variable does not in fact point to any valid memory location
• The special constant NULL is defined for this purpose
• It is important not to dereference a pointer variable that has the value NULL or is not initialized with the * operator
![Page 14: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/14.jpg)
14
Passing Parameters by ValuePassing Parameters by Value
void setToZero(int var){ var = 0;}
main(){ int x;
x = 10; setToZero(x);}
var: 10
x: 10
var: 0
x: 10
![Page 15: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/15.jpg)
15
Passing Parameters by ReferencePassing Parameters by Reference
void setToZero(int *ip){ *ip = 0;}
main(){ int x;
x = 10; setToZero(&x);}
ip:
x: 10
ip:
x: 0
![Page 16: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/16.jpg)
16
An ExampleAn Example
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
![Page 17: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/17.jpg)
17
Returning Multiple ResultsReturning Multiple Results
void convertTimeToHM(int time, int *pHours, int *pMinutes){ *pHours = time / MinutesPerHour; *pMinutes = time % MinutesPerHour;}
main(){ int time, hours, minutes; scanf(“%d”, &time); convertTimeToHM(time, &hours, &minutes); printf(“HH:MM format: %d:%d\n”, hours, minutes);}
![Page 18: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/18.jpg)
18
Don’t Overuse Call by ReferenceDon’t Overuse Call by Referenceint hours(int time){ return time / MinutesPerHour;}int minutes(int time){ return time % MinutesPerHour;}main(){ int time; scanf(“%d”, &time); printf(“HH:MM format: %d:%d\n”, hours(time), minutes(time));}
![Page 19: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/19.jpg)
19
Pointers and ArraysPointers and Arrays
• Pointers can also point to elements of an array
int array[10], *p;p = &array[0];*p = 10;printf(“%d, %d\n”, array[0], *p);
![Page 20: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/20.jpg)
20
Pointer ArithmeticPointer Arithmetic
• If a pointer points to elements of an array, some simple pointer arithmetic is meaningful
• If p points to array[i], p+k points to array[i+k]
• If p points to array[i], p-k points to array[i-k]
• If p points to array[i] and q points to array[j], p-q is equal to i-j
![Page 21: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/21.jpg)
21
Pointer ArithmeticPointer Arithmetic
1.0
2.0
3.0
1016
1000
1000
1008
1016
1024
1028
1032
array[0]
array[1]
array[2]
p1
p2
p1-2, p2
p1-1, p2+1
p1, p2+2
![Page 22: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/22.jpg)
22
An ExampleAn Example
main(){ int i, sum, array[10], *p;
for (i = 0; i < 10; i++) { scanf(“%d”, &array[i]); } sum = 0; for (p = &array[0]; p <= &array[9]; p++) { sum += *p; }}
main(){ int i, sum, array[10];
for (i = 0; i < 10; i++) { scanf(“%d”, &array[i]); } sum = 0; for (i = 0; i < 10; i++) { sum += array[i]; }}
![Page 23: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/23.jpg)
23
++ and --++ and --
• The postfix form: x++uses the value of x as the value of the expression first, and then increments it
• The prefix form: ++xincrements the value of x first, and then uses the new value as the value of the expression
![Page 24: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/24.jpg)
24
An ExampleAn Examplemain(){ int x, y;
x = 5; y = ++x; printf(“x = %d, y = %d\n”, x, y); x = 5; y = x++; printf(“x = %d, y = %d\n”, x, y);}
![Page 25: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/25.jpg)
25
An ExampleAn Example
for (i = 0; i < n; i++) arr[i] = 0;
for (i = 0; i < n;) arr[i++] = 0;
for (i = 0; i < n;) arr[i] = i++;
![Page 26: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/26.jpg)
26
An ExampleAn Example
*p++
(*p)++ *(p++)
![Page 27: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/27.jpg)
27
An ExampleAn Example
main(){ int i, sum, array[10], *p;
for (i = 0; i < 10; i++) { scanf(“%d”, &array[i]); } sum = 0; for (p = array; p <= &array[9];) { sum += *p++; }}
main(){ int i, sum, array[10];
for (i = 0; i < 10; i++) { scanf(“%d”, array+i); } sum = 0; for (i = 0; i < 10; i++) { sum += *(array+i); }}
![Page 28: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/28.jpg)
28
An ExampleAn Exampleint add(int array[], int size){ int i, sum; sum = 0; for (i = 0; i < size; i++) sum += array[i]; return sum;}
main(){ int s, n[SIZE]; s = add(n, SIZE);}
int add(int *array, int size){ int i, sum; sum = 0; for (i = 0; i < size; i++) sum += *(array+i); return sum;}
main(){ int s, n[SIZE]; s = add(n, SIZE);}
![Page 29: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/29.jpg)
29
An ExampleAn Example
main(){ int i, sum, array[10];
for (i = 0; i < 10; i++) { scanf(“%d”, &array[i]); } sum = 0; for (i = 0; i < 10; i++) { sum += array[i]; } printf(“%d\n”, sum);}
main(){ int i, sum, *array;
for (i = 0; i < 10; i++) { scanf(“%d”, array+i); } sum = 0; for (i = 0; i < 10; i++) { sum += *(array+i); } printf(“%d\n”, sum);}
error
error
4 bytes40 bytes
![Page 30: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/30.jpg)
30
Dynamic AllocationDynamic Allocation
• Static allocation: memory spaces that are allocated in fixed locations and persist throughout the entire program
• Automatic allocation: memory spaces that are allocated when entering a function and freed when exiting a function
• Dynamic allocation: memory spaces that are explicitly allocated and freed by programmers while the program is running
![Page 31: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/31.jpg)
31
Memory OrganizationMemory Organization
Static area
Stack area
Heap area
![Page 32: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/32.jpg)
32
Malloc and FreeMalloc and Free
In stdlib.h:
void *malloc(int nBytes);
void free(void *pointer);
void * is a general pointer type
![Page 33: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/33.jpg)
33
Malloc and FreeMalloc and Free
char *cp;cp = (char *) malloc(10 * sizeof(char));free(cp);
cp
int *ip;ip = (int *) malloc(10 * sizeof(int));free(ip);
ip
![Page 34: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/34.jpg)
34
Dynamic ArraysDynamic Arraysmain(){ int i, sum, n, *array;
scanf(“%d”, &n); array = (int *) malloc(n * sizeof(int)); for (i = 0; i < n; i++) scanf(“%d”, array+i); /* scanf(“%d”, &array[i]) */ sum = 0; for (i = 0; i < n; i++) sum += *(array+i); /* sum += array[i] */ printf(“%d\n”, sum); free(array);}
dynamic array
![Page 35: 1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.](https://reader035.fdocuments.net/reader035/viewer/2022062423/56649c765503460f9492ae51/html5/thumbnails/35.jpg)
35
Detecting Errors in MallocDetecting Errors in Mallocmain(){ int i, sum, n, *array;
scanf(“%d”, &n); array = (int *) malloc(n * sizeof(int)); if (array == NULL) { printf(“Error: no more memory\n”); exit(1); } for (i = 0; i < n; i++) scanf(“%d”, array+i); sum = 0; for (i = 0; i < n; i++) sum += *(array+i); printf(“%d\n”, sum);}