Download presentation
Presentation is loading. Please wait.
Published byTyrone Francis Modified over 8 years ago
1
CS 2130 Lecture 6 Pointers and Arrays
2
Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into high- level languages has been a step backwards from which we may never recover. Tony Hoare Famous British computer scientist
3
Pointers are cool! Jim Greenlee Famous American computer scientist
4
Recall A variable associates two things –A symbolic name –A value In actual implementation in computing, a variable typically associates a symbolic name, a physical address and a value Pointers are a special kind of variable where the value stored is the physical address of some other variable or data.
5
Pointers int i; int *ip; Pointer: Variable that contains address of another variable. A pointer is a data object which is separate from what it points to. ip is a pointer to an integer It is not pointing to an integer
6
The Mysterious * Declaration int *ip; The name of the variable being declared is ip Per K & R meaning is that when ip gets dereferenced i.e. *ip, the item referred to is an int. Use ip is the value of the pointer *ip is the value of what it is pointing at. Here the * is an operator
7
Pointers NameContents i Code int i;
8
Pointers NameContents i ip Code int i; int *ip;
9
Pointers NameContents i42 ip Code int i; int *ip; i = 42;
10
Pointers NameContents i42 ip Code int i; int *ip; i = 42; *ip = 84; ERROR!!! Core Dump if lucky ERROR!!! Core Dump if lucky
11
Pointers NameContents i42 ip Code int i; int *ip; i = 42; ip = &i; Address of Operator
12
Pointers NameContents i84 ip Code int i; int *ip; i = 42; ip = &i; *ip = 84
13
Pointers NameContents i?????????? ip Code int i; int *ip; i = 42; ip = &i; *ip = &i; NO!!!
14
Pointers Powerful and dangerous No runtime checking (for efficiency) Bad reputation Java attempts to remove the features of pointers that cause many of the problems hence the decision to call them references –No address of operators –No dereferencing operator (always dereferencing) –No pointer arithmetic
15
Questions?
16
Arrays int ia[6]; Allocates consecutive spaces for 6 integers How much space is allocated?
17
Arrays int ia[6]; Allocates consecutive spaces for 6 integers How much space is allocated? 6 * sizeof(int) Also creates ia which is effectively a constant pointer to the first of the six integers What does ia[4] mean? ia
18
Arrays int ia[6]; Allocates consecutive spaces for 6 integers How much space is allocated? 6 * sizeof(int) Also creates ia which is effectively a constant pointer to the first of the six integers What does ia[4] mean? Multiply 4 by sizeof(int). Add to ia and dereference yielding: ia ia[4]
19
Arrays int ia[6]; Note: ia &ia[0] Never say, "Pointer and arrays are exactly the same thing!!! int *ip; ip = ia;/* Okay */ ia = ip;/* Illegal */
20
sizeof Compile time operator Two forms sizeof object sizeof ( type name ) Returns the size of the object or the size of objects of type name in bytes Note: Parentheses can be used inthe first form with no adverse effects
21
sizeof if sizeof(int) == 4 then sizeof(i) == 4 On a typical 32 bit machine... sizeof(*ip) 4 sizeof(ip) 4 char *cp; sizeof(char) 1 sizeof(*cp) 1 sizeof(cp) 4 int ia[6]; sizeof(ia) 24 Not the same thing!!!
22
Arrays int ia[6]; ia ia[4] means *(ia + 4) which means *(ia + 4 * sizeof(int)) Or *(ia + 4 * sizeof(*ia))
23
Pointer Arithmetic Note on the previous slide when we added the literal 4 to a pointer it actually gets interpreted to mean 4 * sizeof(thing being pointed at) This is why pointers have associated with them what they are pointing at!
24
Arrays int ia[6]; 012345 ia Array elements are numbered like this since that's how the pointer arithmetic works out!
25
Arrays int ia[6]; 012345 ia ia[4] = 42; 42
26
Fun with C int ia[6]; ia[4] = 42; is the same as *(ia + 4) = 42; and since addition is commutative *(4 + ia) = 42; would imply that 4[ia] = 42; should work. Does it? Is it a good idea?
27
Address Operators & & Have a variable and want the address of it. * * Have address (or pointer) and want value of variable that it's pointing at.
28
Pop Quiz II #define MAX 6 int ia[MAX]; int *ip; ip = ia; /* Okay??? */ ia = ip; /* Okay??? */ ip[2] = 87; /* Okay??? */ *ip refers to the first element of ia ip++; increments ip by how much? *ip now refers to what? ia[1]!!!
29
More pointer arithmetic int i; int ia[MAX]; for(i = 0; i< MAX; i++) ia[i] = 0; int *ip; int ia[MAX]; for(ip = ia; ip < ia + MAX; ip++) *ip = 0; Sometimes pointer arithmetic is faster than array manipulation
30
Order of Operations *ip++ = 0; Equivalent to *ip = 0; ip++;
31
Order of Operations *(++ip) = 0; Equivalent to ++ip; *ip = 0;
32
Pointers to Pointers? NameContents i Code int i;
33
Pointers to Pointers? NameContents i ip Code int i; int *ip;
34
Pointers to Pointers? NameContents i ip ipp Code int i; int *ip; int **ipp;
35
Pointers to Pointers? NameContents i42 ip ipp Code int i; int *ip; int **ipp; i = 42;
36
Pointers to Pointers? NameContents i42 ip ipp Code int i; int *ip; int **ipp; i = 42; ip = &i;
37
Pointers to Pointers? NameContents i84 ip ipp Code int i; int *ip; int **ipp; i = 42; ip = &i; *ip = 84;
38
Pointers to Pointers? NameContents i84 ip ipp Code int i; int *ip; int **ipp; i = 42; ip = &i; *ip = 84; ipp = &ip;
39
Pointers to Pointers? NameContents i22 ip ipp Code int i; int *ip; int **ipp; i = 42; ip = &i; *ip = 84; ipp = &ip; **ipp = 22;
40
Arrays of Pointers char *month_name(int n) { static char *name[] = { "Illegal month", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; return (n 12)? name[0]: name[n]; }
41
Hungarian Notation Invented by Charles Simonyi "Some people think it's the best thing since structured programming; others hate Hungarian with a passion." Incredibly simplified version p * –Include in the name of every pointer as many p 's as there are * 's –Example int *pint; char **ppchr; *p –Then when dereferencing, each * cancels a p *pint refers to an int *ppchr refers to a pointer to a char
42
Questions
43
Recall int ia[6]; ia[2] = 42; Address calculation: 2 * sizeof(*ia) + ia Access is by dereferencing *(2 * sizeof(*ia) + ia) 42
44
What happens? int ia[6]; ia[8] = 84; Address calculation: 8 * sizeof(*ia) + ia 4284
45
How does a two dimensional array work? How would you store it? 0123 0 1 2
46
0123 0 1 2 0,00,10,20,31,01,11,21,32,02,12,22,3 Column 0 Column 1 Column 2 Column 3 Column Major Order 0,00,10,20,31,01,11,21,32,02,12,22,3 Row 0 Row 2 Row 1 Row Major Order
47
Advantage Using Row Major Order allows visualization as an array of arrays ia[1] ia[1][2] 0,00,10,20,31,01,11,32,02,12,22,3 1,2 0,00,10,20,31,01,11,32,02,12,22,31,2
48
Recall One Dimensional Array int ia[6]; Address of beginning of array: ia &ia[0] Two Dimensional Array int ia[3][6]; Address of beginning of array: ia &ia[0][0] also Address of row 0: ia[0] &ia[0][0] Address of row 1: ia[1] &ia[1][0] Address of row 2: ia[2] &ia[2][0]
49
Declaration int ia[3][4]; Type Address Number of Rows Number of Columns Declaration at compile time i.e. size must be known Declaration at compile time i.e. size must be known
50
Element Access Given a row and a column index How to calculate location? To skip over required number of rows: row_index * sizeof(row) row_index * Number_of_columns * sizeof(arr_type) This plus address of array gives address of first element of desired row Add column_index * sizeof(arr_type) to get actual desired element 0,00,10,20,31,01,11,21,32,02,12,22,3
51
Element Access Element_Address = Array_Address + Row_Index * Num_Columns * Sizeof(Arr_Type) + Column_Index * Sizeof(Arr_Type) Element_Address = Array_Address + (Row_Index * Num_Columns + Column_Index) * Sizeof(Arr_Type)
52
What if array is stored in Column Major Order? Element_Address = Array_Address + (Column_Index * Num_Rows + Row_Index) * Sizeof(Arr_Type) 0,00,10,20,31,01,11,21,32,02,12,22,3
53
Don't confuse Arrays int ia[10]; Arrays of pointers char *names[13]; Pointers to pointers Node **head; Multidimensional arrays int ia[3][4][5]; As formal parameter int iarr[] As formal parameter char *nms[] Pointers to pointers Node **curr; Multidimensional arrays int iar[][4][5]
54
Multidimensional Example void tester(int arr[][4][5]) { } int main() { int ia[3][4][5]; int ib[8][4][5]; int *ic; ic = ia; tester(ia); tester(ib); tester(ic); return 0; }
55
Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.