Download presentation
Presentation is loading. Please wait.
1
Thursday, January 11, 2007 Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined Harrisberger's Fourth Law of the Lab: Experience is directly proportional to the amount of equipment ruined.
2
Most of C++’s power is derived from pointers. They allow different sections of code to share information easily. Pointers enable complex data structures like linked lists. Pointers
3
Assumptions: characters are one byte in length integers are four bytes long floats are four bytes long doubles are eight bytes long Pointers
4
A pointer is a variable that holds a memory address. Pointers
5
int x=5; int *xptr; 5 x of type int0x0012F578 0x0012F690xptr of type int*
6
int x=5; int *xptr; xptr=&x; //points to x 5 0x0012F578 x of type int0x0012F578 0x0012F690xptr of type int*
7
int x=5; int *xptr; xptr=&x; //points to x 5 x of type int0x0012F578 xptr of type int*
8
int x=5; int *xptr; xptr=&x; //points to x 5 x of type intxptr of type int*
9
int x=5; int *xptr; xptr=&x; //points to x The most common error is forgetting to initialize the pointer 5 x of type intxptr of type int*
10
There are two special operators that are used with pointers: * and & The & is a unary operator that returns the memory address of its operand. int balance = 350; int *balptr; balptr = &balance; Pointer Operators
11
The second operator * is the complement of &. It is a unary operator that returns the value of variable located at address specified by its operand. int balance = 350; int *balptr; balptr = &balance; int value; value = *balptr; //what does value contain? Pointer Operators
12
& address of operator * value of pointee (de-referencing operator) Pointer Operators
13
When a pointer is first allocated it does not point to anything. Trying to de-reference an un-initialized pointer is a serious runtime error. If you are lucky the dereference will crash or halt immediately. If you are unlucky it will corrupt a random area of memory – so that things go wrong after some indefinite time. Pointers
14
Make a memory drawing! Pointers
15
int main() { int balance; int *balptr; int value; balance = 3200; balptr = &balance; value = *balptr; cout << "balance is: " << value << '\n'; cout<< "Memory address where balance is stored is: " <<balptr<<endl; return 0; } Pointer Operators
16
Output balance is: 3200 Memory address where balance is stored is: 0012FF7C (Note memory address may be different when you run it on your machine)
17
§Another example §Three things to remember…
18
int a, b; int *aptr, bptr; //Be careful!
19
value = *balptr; The compiler transfers the proper number of bytes according to base type. int *p; double f; //... p = &f; // ERROR Base Type
20
int *p; int x=12; p=&x; cout<<x<<endl; //Assign a value to the location pointed to by p *p = 101; cout<<x<<endl;//what is value of x? cout<<*p<<endl; cout<<p<<endl; cout<<&x<<endl; Assigning values through a pointer
21
int *p; int x=12; p=&x; cout<<x<<endl;//prints 12 //Assign a value to the location pointed to by p *p = 101; cout<<x<<endl; //prints 101 cout<<*p<<endl; //prints 101 cout<<p<<endl; //prints 0012FF78 cout<<&x<<endl; //prints 0012FF78 (Note memory address may be different when you run it on your machine) Assigning values through a pointer
22
(*p)++; //increment value at the location pointed to by p int main() { int *p, num; p = # *p = 454; cout << num << ' '; (*p)++; /*parentheses are necessary because * operator has lower precedence than ++ operator */ cout << num << ' '; (*p)--; cout << num << '\n'; return 0; } //Output? Assigning values through a pointer
23
//Output is 454 455 454 int main() { int *p, num; p = # *p = 454; cout << num << ' '; (*p)++; cout << num << ' '; (*p)--; cout << num << '\n'; return 0; } Assigning values through a pointer
24
There are only four arithmetic operators that can be used on pointers: ++, --, +, - Let p1 be an integer pointer which contains the address 5000 p1++; Each time p1 is incremented, it shall point to the next integer. p1--; Pointer Arithmetic
25
There are only four arithmetic operators that can be used on pointers: ++, --, +, - Let p1 be an integer pointer which contains the address 5000 p1++;// now p1 will be 5004 Each time p1 is incremented, it shall point to the next integer. p1--; will cause p1 to be 4996 if initially it was 5000. Pointer Arithmetic
26
int main() { int *aptr, a[5]; double *bptr, b[5]; int i; aptr = a; bptr = b; for(i=0; i<5; i++){ aptr = a+i; bptr = b+i; cout << aptr << " " << bptr << endl; } return 0; } Pointer Arithmetic
27
0x0012FEB8 0x0012FE7C 0x0012FEBC 0x0012FE84 0x0012FEC0 0x0012FE8C 0x0012FEC4 0x0012FE94 0x0012FEC8 0x0012FE9C Pointer Arithmetic
28
Let p1 be an char pointer which contains the address 4000 p1++;// now p1 will be 4001 Each time p1 is incremented, it shall point to the next character. p1--; will cause p1 to be 3999 if initially it was 4000. Pointer of type other than char shall increase or decrease by length of base type. Pointer Arithmetic
29
You cannot add two pointers You can subtract two pointers (if they are of same base type). Other than addition or subtraction of a pointer and an integer, or the subtraction of two pointers, no other arithmetic operations can be performed on pointers. You cannot add or subtract float or double values. Pointer Arithmetic
30
int main() { int *aptr, a[5]; double *bptr, b[5]; int i; aptr = a; bptr = b; for(i=0; i<5; i++){ a = aptr+i; b = bptr+i; cout << a << " " << b << endl; } return 0; } What is wrong here?
31
In C++, there is a close relationship between pointers and arrays. Pointers and Arrays
32
In C++, there is a close relationship between pointers and arrays. Name of array, without an index, is a pointer to first element of the array. Pointers and Arrays
33
int main() { int *i; int i_array[4]={55, 26, 17, 68}; i=i_array; cout << i << " " << i_array << “\n”; cout << i[0] << " " << i_array[0] << “\n”; cout << *(i+1) << " " << i_array[1] <<“\n”; return 0; } //Output? Pointers and Arrays
34
Output is: 0012FF6C 55 26 Pointers and Arrays
35
int i1[4]={55,26,17,68}; int i2[4]={11, 2, 53, 14}; i2=i1; //NOT ALLOWED char str1[]=“i am a string”; char str2[]=“i am a string”; Pointers may be compared in C++ using relational operators like >, >=, <, <=, == etc, but they must have some relationship to be meaningful Pointers and Arrays
36
int i1[4]={55,26,17,68}; int i2[4]={11, 2, 53, 14}; i2=i1; //NOT ALLOWED Why? /*name of array is a constant that points to beginning of array*/ char str1[]=“i am a string”; char str2[]=“i am a string”; str1==str2; //WRONG way of string comparison Pointers may be compared in C++ using relational operators like >, >=, <, <=, == etc, but they must have some relationship to be meaningful Pointers and Arrays
37
int i1[4]={55,26,17,68}; int i2[4]={11,2,53,14}; int *p1; p1 = i1; int *p2; p2 = p1; //this is ok Pointers and Arrays
38
int *y_ptr, y; //y is of type int // y_ptr is pointer to int y=45; y_ptr=&y; int *y_ptr, *another_ptr, y=45; y_ptr=&y; another_ptr=&y; /*what is *another_ptr what is *y_ptr*/ Pointers
39
int b[] = {10, 20, 30, 40}; int *bPtr; bPtr = b; // set bPtr to point to array b cout << "Array b printed with:" << endl << "Array subscript notation" << endl; for (int i = 0; i < 4; i++) cout <<b[i] << endl; cout << "Pointer subscript notation" << endl; for (i = 0; i < 4; i++) cout << bPtr[i] << endl; //Another way to do this? Using subscripting and pointer notations with arrays
40
int offset; cout << "Pointer/offset notation where" << endl << "the pointer is the array name" << endl; for(offset = 0; offset < 4; offset++) cout << *(b + offset) << endl; cout <<"Pointer/offset notation" << endl; for(offset = 0; offset < 4; offset++) cout << *(bPtr + offset) << endl; Using subscripting and pointer notations with arrays
41
int *i; int int_array[4]={5,6,7,8}; i=int_array; cout << i << " " << int_array<<endl; cout << i[0] << " " << int_array[0]<< endl ; cout << &i[0] << " " << &int_array[0]<< endl; Using subscripting and pointer notations with arrays
42
0x0012FEBC 5 0x0012FEBC Using subscripting and pointer notations with arrays
43
int a[5]={12, 3, 45, 5, 8}; int *ptr; ptr=a; cout<<a[0]<<endl; cout<<ptr[0]<<endl; cout<<*(a+0)<<endl; cout<<*(ptr+0)<<endl; cout<<a[1]<<endl; cout<<ptr[1]<<endl; cout<<*(a+1)<<endl; cout<<*(ptr+1)<<endl; Using subscripting and pointer notations with arrays
44
int a[5]={12, 3, 45, 5, 8}; int *ptr; ptr=a; cout<<a[0]<<endl; //prints 12 cout<<ptr[0]<<endl; //prints 12 cout<<*(a+0)<<endl; //prints 12 cout<<*(ptr+0)<<endl; //prints 12 cout<<a[1]<<endl; //prints 3 cout<<ptr[1]<<endl; //prints 3 cout<<*(a+1)<<endl; //prints 3 cout<<*(ptr+1)<<endl; //prints 3 Using subscripting and pointer notations with arrays
45
int i1[4]={55, 26, 17, 68}; What is wrong with the following statement? i1++; //The following is ok int *i_ptr; i_ptr = i1; i_ptr++; cout<<*i_ptr; //The following is ok *(i1+3) = 100; // This is OK because i1 has not changed Pointers and Arrays
46
int b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int *bPtr; bPtr= b; // set bPtr to point to array b /* set b2Ptr to point to sixth element of array b */ int *b2ptr; b2ptr = b+5; //Remember pointer arithmetic? cout << "b[" << 5 << "] = " << b[5] << endl; cout << "*b2ptr= " << *b2ptr << endl; cout << "(b2ptr - bPtr) = " << (b2ptr - bPtr); Pointers and Arrays
47
int b[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; int *bPtr; bPtr= b; // set bPtr to point to array b /* set b2Ptr to point to sixth element of array b */ int *b2ptr; b2ptr = b+5; //Remember pointer arithmetic? cout << "b[" << 5 << "] = " << b[5] << endl; cout << "*b2ptr= " << *b2ptr << endl; cout << "(b2ptr - bPtr) = " << (b2ptr - bPtr) << endl; Output is: b[5] = 60 *b2ptr= 60 (b2ptr - bPtr) = 5 Pointers and Arrays
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.