Bài giảng Introduction to computer programming - Chapter 8: Pointers

Ho Chi Minh City University of Technology  
Faculty of Computer Science and Engineering  
Chapter 8: Pointers  
Introduction to Computer Programming  
(C language)  
TS. Võ Thị Ngọc Châu  
2017 – 2018, Semester 2  
Course Content  
C.1. Introduction to Computers and  
Programming  
C.2. C Program Structure and its  
Components  
C.3. Variables and Basic Data Types  
C.4. Selection Statements  
C.5. Repetition Statements  
C.6. Functions  
C.7. Arrays  
C.8. Pointers  
C.9. File Processing  
2
References  
[1] “C: How to Program”, 7th Ed. – Paul  
Deitel and Harvey Deitel, Prentice Hall, 2012.  
[2] “The C Programming Language”, 2nd Ed.  
– Brian W. Kernighan and Dennis M. Ritchie,  
Prentice Hall, 1988  
and others, especially those on the Internet  
3
Content  
Introduction  
Declare and initialize pointers  
Operations on pointers  
Pointers and arrays  
Variable storage and heap memory  
Memory allocation and de-allocation  
Pointers and structures  
Pass pointers to a function  
Function pointers  
4
Summary  
Introduction - Recall - Chapter 2  
Main memory is addressable continuously.  
scanf() for input data from input devices to main memory  
Input a string: Input  
Input a character: A  
Input an integer: -123  
Input device = keyboard  
Input  
Input device = keyboard  
A
Input device = keyboard  
-123  
Input  
A
-123  
Main memory ≈ variable  
Main memory ≈ variable  
Main memory ≈ variable  
Varying size: user-predefined Fixed sizes: character = 1 byte, integer = 4 bytes, …  
char aString[5];  
…
scanf(“%s”, aString)  
char aChar;  
…
scanf(“%c”, &aChar)  
int anInteger;  
…
scanf(“%d”, &anInteger)  
printf(“%s”, aString)  
printf(“%c”, aChar)  
printf(“%d”, anInteger)  
Introduction - Recall - Chapter 3  
Built-in data types (primitive/fundamental)  
char (signed char), unsigned char  
short int, unsigned short, int, unsigned int, long int,  
unsigned long int, long long int, unsigned long long  
float, double, long double  
void  
enum (enumerated data associated with integers)  
Derived data types  
We haven‟t discussed  
arrays [] of objects of a given type  
this derived data type  
in detail yet!!!  
pointers * to objects of a given type  
structures struct containing objects of other types  
union containing any one of several objects of  
6
various types  
Introduction - Recall - Chapter 3  
Name  
Operator  
Description  
Example  
sizeof  
sizeof(type),  
sizeof(variable)  
Returns the size (bytes) of sizeof(char)  
a type or a variable  
int anInt = 0;  
sizeof(anInt);  
address  
&Variable  
Returns the address of the char aChar;  
memory named Variable  
char* ptrChar;  
ptrChar = &aChar;  
Dereferencing  
Index  
*Pointer  
Returns the value of the  
aChar = *ptrChar + 1;  
memory Pointer points to  
Variable[..]  
Returns the element at the int intArray[3];  
index  
intArray[0] = 0;  
intArray[1] = 1;  
intArray[2] = 2;  
anInt = intArray[1];  
Structure  
member  
Structure_  
name.member  
Refers to a member of a  
particular structure  
struct point pt;  
pt.x = 10;  
Introduction - Recall - Chapter 6  
A function to swap two integer numbers  
void swap(int a, int b){  
int temp;  
a and b will be passed by  
temp = a;  
values of int type.  
a = b;  
b = temp;  
}
void swap(int *a, int *b){  
int temp;  
a and b will be passed by pointers  
to int values, i.e. addresses of the  
memory that contains int values.  
temp = *a;  
*a = *b;  
*b = temp;  
}
8
Introduction - Recall - Chapter 6  
Stack‟s values when a=10 and  
b=9 in the main() function  
Callee‟s stack  
Caller‟s stack  
000000000022FE4C  
&a  
000000000022FE4C  
000000000022FE48  
10  
9
a
b
a
000000000022FE48  
&b  
b
temp  
Stack‟s values  
before the callee  
ends  
Callee‟s stack  
Caller‟s stack  
000000000022FE4C  
&a  
&b  
000000000022FE4C  
000000000022FE48  
9
a
b
a
000000000022FE48  
10  
b
10  
temp  
9
Introduction - Recall - Chapter 7  
int a[10] = {2, 3, -1, 0, 4, 7, 9};  
a, array name, is the address of the first int memory location.  
Access to a[5] returns an  
int value: 4.  
2
3
-1  
0
4
7
9
0
0
0
index  
0
1
2
3
4
5
6
7
8
9
int b[3][5] = {{2, 3, -1, 0, 4}, {7, 9}, {6, 11, -2, 5}};  
b, array name, is the address of the first int memory location.  
Access to b[2] returns  
the address of b[2][0]  
for the third row.  
2
3
-1  
0
4
7
9
0
0
0
6
11  
-2  
5
0
index  
[0][0] [0][1] [0][2] [0][3] [0][4] [1][0] [1][1] [1][2] [1][3] [1][4] [2][0] [2][1] [2][2] [2][3] [2][4]  
Access to b[0][2]  
returns an int value: -1.  
10  
Introduction - Recall - Chapter 7  
Pass a value of an element at index i of a one-dimension array a  
to functions  
Value passing - unchanged  
Call to function func: func(a[i], …)  
Pass all the values of the elements of a one-dimension array a  
to functions  
Address passing - changeable  
Call to function func: func(a, …)  
Pass a value of an element at indices i and j of a two-dimension  
array b to functions  
Value passing - unchanged  
Call to function func: func(b[i][j], …)  
Pass a row at index i of a two-dimension array b to functions  
Address passing - changeable  
Call to function func: func(b[i], …)  
Pass all the values of the elements of a two-dimension array b  
to functions  
Address passing - changeable  
Call to function func: func(b, …)  
11  
Introduction  
Related to physical memory addresses,  
pointers are provided as  
A means for manipulation on memory  
A means for pass-by-reference implementation  
A means for dynamic data structures that can  
grow and shrink at execution time  
In Memory  
[1], Figure 7.1, pp. 254  
12  
In Memory  
In Memory  
Declare and initialize pointers  
A pointer of a data type  
A variable whose value is an address of memory  
location that contains a value of a data type  
Must be declared before its use  
optionally with an initial value  
type_name* variable_name =opt expressionopt;  
- variable_name: a valid identifier for a pointer  
- type_name: a valid data type (basic, derived, a new one with  
typedef)  
- type_name*: a pointer type for pointers that point to memory  
location of a data type type_name  
- expression: an initial value (0, NULL, an address)  
13  
Declare and initialize pointers  
type_name* variable_name =opt expressionopt;  
// a variable of the char data type  
// an un-initialized pointer pointing to a char  
// a null pointer pointing to a char  
// a null pointer pointing to a char  
// a pointer pointing to a char with an  
initialized pointer to aChar  
pChar1  
pChar2  
pChar3  
pChar4  
???????  
000000  
000000  
22FE37  
22FE37  
a
aChar  
14  
Declare and initialize pointers  
type_name* variable_name =opt expressionopt;  
// a variable of the int data type  
// an un-initialized pointer pointing to an int  
// a null pointer pointing to an int  
// a null pointer pointing to an int  
// a pointer pointing to an int with an  
initialized pointer to anInt  
pInt1  
pInt2  
pInt3  
pInt4  
???????  
000000  
000000  
22FE38  
22FE38  
10  
anInt  
15  
Declare and initialize pointers  
type_name* variable_name =opt expressionopt;  
// a variable of the float data type  
// an un-initialized pointer pointing to a float  
// a null pointer pointing to a float  
// a null pointer pointing to a float  
// a pointer pointing to a float with an  
initialized pointer to aFloat  
pFloat1  
pFloat2  
pFloat3  
pFloat4  
???????  
000000  
000000  
22FE3C  
22FE3C  
10.5  
aFloat  
16  
Declare and initialize pointers  
type_name* variable_name =opt expressionopt;  
// a variable of the point3D data type  
// an un-initialized pointer pointing to a point3D  
// a null pointer pointing to a point3D  
// a null pointer pointing to a point3D  
// a pointer pointing to a point3D with an  
initialized pointer to aPoint  
pPoint1  
pPoint2  
pPoint3  
pPoint4  
???????  
000000  
000000  
22FE40  
22FE40  
1.0 2.0 3.0  
aPoint  
x
y
z
17  
Declare and initialize pointers  
22FE37  
ptrChar  
22FE37  
a
aChar  
22FE30  
anInt  
ptrInt  
22FE30  
22FE30  
9
ptrInteger  
an int value in the memory  
an address of the memory  
location named anInt: 9  
location named anInt: 22FE30  
an address of the memory location  
an int value in the memory  
18  
pointed by ptrInt: 22FE30  
location pointed by ptrInt: 9  
Declare and initialize pointers  
A pointer of a data type  
A variable whose value is an address of memory  
location that contains a value of a data type  
What if a particular data type is not told?  
Pointers to void  
void * pointer;  
19  
Declare and initialize pointers  
Pointers to void  
The generic pointer type which is a flexible means  
to manipulate memory for any data type  
Adaptive data structures  
Function definitions  
A pointer to any data type can be assigned  
directly to a pointer of type void *.  
A pointer of type void * can be assigned directly  
to a pointer to any data type.  
A pointer of type void * can be casted to any data  
type.  
A void * pointer cannot be dereferenced.  
20  
Tải về để xem bản đầy đủ
pdf 86 trang yennguyen 20040 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Introduction to computer programming - Chapter 8: Pointers", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_introduction_to_computer_programming_chapter_8_poi.pdf