Tải bản đầy đủ (.pdf) (86 trang)

Lecture 8 pointers

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.79 MB, 86 trang )

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
 Summary


4


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


Main memory ≈ variable

A

Main memory ≈ variable

-123

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
pointers * to objects of a given type in detail yet!!!
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
a type or a variable

sizeof(char)
int anInt = 0;

sizeof(anInt);

address

&Variable

Returns the address of the
memory named Variable

char aChar;

char* ptrChar;
ptrChar = &aChar;

Dereferencing

*Pointer

Returns the value of the
memory Pointer points to

Index

Variable[..]

Returns the element at the
index

aChar = *ptrChar + 1;

int intArray[3];
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;

7


Introduction - Recall - Chapter 6
A function to swap two integer numbers
void swap(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}

a and b will be passed by

values of int type.

void swap(int *a, int *b){
int temp;
a and b will be passed by pointers
temp = *a;
to int values, i.e. addresses of the
*a = *b;
memory that contains int values.
*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

000000000022FE48

10
9

a &a
b


&b

000000000022FE4C

a

000000000022FE48

b
temp

Stack‟s values
before the callee
ends
Callee‟s stack

Caller‟s stack
000000000022FE4C

000000000022FE48

9
10

a &a
b

&b

000000000022FE4C


a

000000000022FE48

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.

index

2

3

-1

0

4

7


9

0

0

0

0

1

2

3

4

5

6

7

8

9

Access to a[5] returns an

int value: 4.

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
index

3

-1

0

4

7

9

0

0

0

6


11

-2

5

0

[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




Pass all the values of the elements of a one-dimension array a
to functions





Call to function func: func(b[i][j], …)

Value passing - unchanged

Pass a row at index i of a two-dimension array b to functions




Call to function func: func(a, …)

Address passing - changeable

Pass a value of an element at indices i and j of a two-dimension
array b to functions




Call to function func: func(a[i], …)

Call to function func: func(b[i], …)

Address passing - changeable

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

In Memory


In Memory

12


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

000000

pChar3

000000

pChar4

22FE37

22FE37
aChar


a
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

000000

pInt3

000000

pInt4

22FE38


22FE38
anInt

10
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

000000

pFloat3

000000

pFloat4


22FE3C

22FE3C
aFloat

10.5
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

000000

pPoint3

000000


pPoint4

22FE40

22FE40
aPoint

1.0

2.0

3.0

x

y

z

17


Declare and initialize pointers

ptrChar

22FE37

22FE37


aChar
ptrInt

22FE30

ptrInteger 22FE30
an int value in the memory
location named anInt: 9

an address of the memory location
pointed by ptrInt: 22FE30

a

22FE30
anInt

9

an address of the memory
location named anInt: 22FE30

an int value in the memory
location pointed by ptrInt: 9

18


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


Pointers to void

Dereference of a void* pointer:

*pVoid should be: *((int*)pVoid)

21


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 data type is a pointer type?



Pointers to pointers


A pointer that points to the memory location
whose value is an address
A conventional variable

A pointer to an int memory location
Pointers to pointers
22





Pointers to pointers

int*** pInt3;
22FE28

pInt3

22FE30

int** pInt2;
22FE30
pInt2

22FE38

int* pInt1;
22FE38
pInt1

22FE44

int anInt;
22FE44

anInt
***pInt3 returns 10


**pInt2 returns 10

*pInt1 returns 10

10

anInt returns 10


Declare and initialize pointers


const



A qualifier to inform the compiler that the value
of a particular variable should not be modified
Recall – Constant variables in Chapter 3
const short MAX = 50;

INVALID!

Modification on constant variable: MAX = 55;


A pointer of a data type



A variable whose value is an address of memory
location that contains a value of a data type

How to apply const

to a pointer?

int* pInt;
22FE38
pInt

22FE44

int anInt;
22FE44
anInt

10

24


Declare and initialize pointers
How to apply const
to a pointer?
YES!!!

int* pInt;
22FE38
pInt


22FE44

int anInt;
22FE44
anInt

Can it be constant?

10

Can it be constant?

Can it be constant?



A non-constant pointer to non-constant data


As normal with no const qualifier

A constant pointer to non-constant data
 A non-constant pointer to constant data
 A constant pointer to constant data


25



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×