Danh mục

Lecture Charter 7: C Pointers

Số trang: 95      Loại file: pdf      Dung lượng: 1.09 MB      Lượt xem: 2      Lượt tải: 0    
Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Lecture "Charter 7: C Pointers" provides students with the knowledge: Pointer Operators, pointer variable definitions and initialization, passing arguments to functions by reference, using the const qualifier with pointers, sizeof operator,... Inviting you refer.
Nội dung trích xuất từ tài liệu:
Lecture Charter 7: C Pointers 17 C Pointers  2007 Pearson Education, Inc. All rights reserved. 27.1 Introduction7.2 Pointer Variable Definitions and Initialization7.3 Pointer Operators7.4 Passing Arguments to Functions by Reference7.5 Using the const Qualifier with Pointers7.6 Bubble Sort Using Call-by-Reference7.7 sizeof Operator7.8 Pointer Expressions and Pointer Arithmetic7.9 Relationship between Pointers and Arrays7.10 Arrays of Pointers7.11 Case Study: Card Shuffling and Dealing Simulation7.12 Pointers to Functions  2007 Pearson Education, Inc. All rights reserved. 37.1 Introduction  Pointers – Powerful, but difficult to master – Simulate call-by-reference – Close relationship with arrays and strings  2007 Pearson Education, Inc. All rights reserved. 47.2 Pointer Variable Definitions andInitialization  Pointer variables – Contain memory addresses as their values – Normal variables contain a specific value (direct reference) – Pointers contain address of a variable that has a specific value (indirect reference) – Indirection – referencing a pointer value  2007 Pearson Education, Inc. All rights reserved. 5Fig. 7.1 | Directly and indirectly referencing a variable.  2007 Pearson Education, Inc. All rights reserved. 67.2 Pointer Variable Definitions andInitialization  Pointer definitions – * used with pointer variables int *myPtr; – Defines a pointer to an int (pointer of type int *) – Multiple pointers require using a * before each variable definition int *myPtr1, *myPtr2; – Can define pointers to any data type – Initialize pointers to 0, NULL, or an address - 0 or NULL – points to nothing (NULL preferred)  2007 Pearson Education, Inc. All rights reserved. 7Common Programming Error 7.1The asterisk (*) notation used to declare pointervariables does not distribute to all variablenames in a declaration. Each pointer must bedeclared with the * prefixed to the name; e.g., ifyou wish to declare xPtr and yPtr as intpointers, use int *xPtr, *yPtr;.  2007 Pearson Education, Inc. All rights reserved. 8Good Programming Practice 7.1Include the letters ptr in pointer variablenames to make it clear that these variablesare pointers and thus need to be handledappropriately.  2007 Pearson Education, Inc. All rights reserved. 9Error-Prevention Tip 7.1 Initialize pointers to prevent unexpected results.  2007 Pearson Education, Inc. All rights reserved. 107.3 Pointer Operators  & (address operator) – Returns address of operand int y = 5; int *yPtr; yPtr = &y; /* yPtr gets address of y */ yPtr “points to” y  2007 Pearson Education, Inc. All rights reserved. 11Fig. 7.2 | Graphical representation of a pointer pointing to an integer variable in memory.  2007 Pearson Education, Inc. All rights reserved. 12Fig. 7.3 | Representation of y and yPtr in memory.  2007 Pearson Education, Inc. All rights reserved. 137.3 Pointer Operators  * (indirection/dereferencing operator) – Returns a synonym/alias of what its operand points to – *yptr returns y (because yptr points to y) – * can be used for assignment - Returns alias to an object *yptr = 7; /* changes y to 7 */ – Dereferenced pointer (operand of *) must be an lvalue (no constants)  * and & are inverses – They cancel each other out  2007 Pearson Education, Inc. All rights reserved. 14Common Programming Error 7.2Dereferencing a pointer that has not beenproperly initialized or that has not beenassigned to point to a specific location inmemory is an error. This could cause a fatalexecution-time error, or it could accidentallymodify important data and allow the programto run to completion with incorrect results.  2007 Pearson Education, Inc. All rights reserved.1 /* Fig. 7.4: fig07_04.c 152 Using the & and * operators */3 #include Outline45 int m ...

Tài liệu được xem nhiều: