0
Upcoming SlideShare
×

# Pointers+(2)

1,576

Published on

Published in: Education, Technology
1 Comment
2 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• good slides

Are you sure you want to  Yes  No
Views
Total Views
1,576
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
223
1
Likes
2
Embeds 0
No embeds

No notes for slide

### Transcript of "Pointers+(2)"

1. 1. Pointers in C C++ Spring 2000 Arrays
2. 2. Pointers <ul><li>A pointer is a variable that holds the address of something else. </li></ul>C++ Spring 2000 Arrays int foo; int *x; foo = 123; x = &foo; foo x 123 3 ... ... MEMORY 0 1 2 3 4 5 81345 81346 81347 Address
3. 3. Pointers <ul><li>Example: int *x=i; </li></ul><ul><li>x is a pointer to an integer. </li></ul>C++ Spring 2000 Arrays “ the int x points to”
4. 4. Pointers <ul><li>In C you can get the address of a variable with the “&” operator. </li></ul>&foo means “the address of foo” foo int foo; foo = 123; x = &foo; 123 ... ... MEMORY 0 1 2 3 4 5 Address
5. 5. Assigning a value to a dereferenced pointer <ul><li>A pointer must have a value before you can dereference it (follow the pointer). </li></ul>C++ Spring 2000 Arrays int *x; *x=3; int foo; int *x; x = &foo; *x=3; ERROR!!! x doesn’t point to anything!!! this is fine x points to foo
6. 6. Pointers to anything <ul><li>int *x; </li></ul><ul><li>int **y; </li></ul><ul><li>double *z; </li></ul>C++ Spring 2000 Arrays x some int some *int some int y z some double
7. 7. Pointer arithmetic <ul><li>Integer math operations can be used with pointers. </li></ul><ul><li>If you increment a pointer, it will be increased by the size of whatever it points to. </li></ul>C++ Spring 2000 Arrays int a[5]; a[0] a[1] a[2] a[3] a[4] int *ptr = a; *ptr *(ptr+2) *(ptr+4)
8. 8. Operations on Pointers <ul><li>Can be done, Valid Operations </li></ul><ul><ul><li>Addition/Subtraction of a number to/from pointer </li></ul></ul><ul><ul><li>Subtraction of two pointers </li></ul></ul><ul><ul><li>Comparison of two pointers </li></ul></ul><ul><ul><li>Assignment of a pointer to another pointer </li></ul></ul><ul><li>Can NOT be done, Invalid Operations </li></ul><ul><ul><li>Addition of two pointers </li></ul></ul><ul><ul><li>Multiplication/Division of a pointer with a number </li></ul></ul>C++ Spring 2000 Arrays
9. 9. Pointer arithmetic <ul><li>You can use the integer x points to in a C expression like this: </li></ul><ul><li>y = *x + 17; </li></ul><ul><ul><li>*x = *x +1; </li></ul></ul>C++ Spring 2000 Arrays
10. 10. Q&A <ul><li>Q. Are the expression *ptr++ and ++*ptr same? </li></ul><ul><li>No. *ptr++ increment the pointer and not the value pointed by it, whereas ++*ptr increments the value pointed to by ptr. </li></ul><ul><li>Q. Can you write any another expression which does the same job as ++*ptr? </li></ul><ul><li>A. (*ptr)++ </li></ul>C++ Spring 2000 Arrays
11. 11. Pointers and Arrays <ul><li>An array name is basically a const pointer. </li></ul><ul><li>You can use the [] operator with a pointer: </li></ul><ul><ul><li>int *x; </li></ul></ul><ul><ul><li>int a[10]; </li></ul></ul><ul><ul><li>x = &a[2]; </li></ul></ul>C++ Spring 2000 Arrays x is “the address of a[2] ”
12. 12. Printing an array using Pointer <ul><li>void print_array(int a[], int len) { </li></ul><ul><li>for (int i=0;i<len;i++) </li></ul><ul><li>cout << &quot;[&quot; << i << &quot;] = &quot; </li></ul><ul><li><< a[i] << endl; </li></ul><ul><li>} </li></ul>C++ Spring 2000 Arrays void print_array(int *a , int len) { for (int i=0;i<len;i++) cout << &quot;[&quot; << i << &quot;] = &quot; << *a++ << endl; } pointer version array version
13. 13. Q&A <ul><li>What would be the output of the following? </li></ul><ul><li>main() </li></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>int arr[]={12,13,14,15,16}; </li></ul></ul><ul><ul><li>printf(“%d %d” %d”, sizeof(arr),sizeof(*arr),sizeof(arr[0])); } </li></ul></ul><ul><ul><li>10 2 2 </li></ul></ul><ul><ul><li>2 2 2 </li></ul></ul><ul><ul><li>10 10 10 </li></ul></ul><ul><ul><li>2 10 2 </li></ul></ul><ul><ul><li>Answer: A </li></ul></ul>C++ Spring 2000 Arrays
14. 14. Q&A <ul><li>Q. What would be equivalent expression for referring the same element as a[i][j][k][l]? </li></ul><ul><li>A. *(*(*(*(a+i)+j)+k)+l) </li></ul>C++ Spring 2000 Arrays
15. 15. <ul><li>Pointers and Functions </li></ul>C++ Spring 2000 Arrays
16. 16. Call by Value <ul><li>main( ) </li></ul><ul><li>{ </li></ul><ul><li>int a = 4; </li></ul><ul><li>myFunc (a); </li></ul><ul><li>} </li></ul><ul><li>myFunc (int b) </li></ul><ul><li>{ </li></ul><ul><li>b = b * 2; </li></ul><ul><li>} </li></ul>Address (2 bytes) 65514 65515 65516 65517 65518 65519 65520 65521 65522 65523 65524 65525 a 4 b 4 8 main myFunc
17. 17. Call by Reference <ul><li>main( ) </li></ul><ul><li>{ </li></ul><ul><li>int a = 4; </li></ul><ul><li>myFunc (&a); </li></ul><ul><li>} </li></ul><ul><li>myFunc (int *b) </li></ul><ul><li>{ </li></ul><ul><li>*b = *b * 2; </li></ul><ul><li>} </li></ul>Data Word Size 1 byte (assume) Address Word Size (2 bytes) 65514 65515 65516 65517 65518 65519 65520 65521 65522 65523 65524 65525 a 4 main myFunc 8 b 65520
18. 18. Passing pointers as parameters <ul><li>void swap( int *x, int *y) { </li></ul><ul><li>int temp; </li></ul><ul><li>temp = *x; </li></ul><ul><li>*x = *y; </li></ul><ul><li>*y = temp; </li></ul><ul><li>} </li></ul>C++ Spring 2000 Arrays
19. 19. Pointer Parameters <ul><li>Pointers are passed by value (the value of a pointer is the address it holds). </li></ul><ul><li>If we change what the pointer points to the caller will see the change. </li></ul><ul><li>If we change the pointer itself, the caller won't see the change (we get a copy of the pointer) </li></ul>C++ Spring 2000 Arrays
20. 20. Q&A <ul><ul><ul><ul><li>main() </li></ul></ul></ul></ul><ul><ul><ul><ul><li>{ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int a[3][4]={ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>1,2,3,4, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>5,6,7,8, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>7,8,9,0 }; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int *ptr; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>ptr=&a[0][0]; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>func(&ptr); } </li></ul></ul></ul></ul><ul><ul><ul><ul><li>void func(int **p) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>{ printf(&quot;%d&quot;,**p);} </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Answer: 1 </li></ul></ul></ul></ul>C++ Spring 2000 Arrays
21. 21. Pointers and String <ul><li>Declared and initialized as: </li></ul><ul><ul><li> char str[6]=“c prog”; // compiler insert null ‘0’ char at the end </li></ul></ul><ul><ul><li>Or </li></ul></ul><ul><ul><li> char *str=“c prog”; //stores address in the pointer variable </li></ul></ul><ul><ul><li>Or </li></ul></ul><ul><ul><li> char *str; </li></ul></ul><ul><ul><li> str=“hello”; //Not a String copy </li></ul></ul><ul><ul><li>Display the content as: </li></ul></ul><ul><ul><li> printf(“%s”,str); </li></ul></ul><ul><ul><li>Or puts(str); </li></ul></ul>
22. 22. Pointers and String <ul><li>Length of string </li></ul><ul><li>char name[20]; char *str; </li></ul><ul><li>str=name; </li></ul><ul><li>while(ptr!=‘0 ’)// condition true until end of the string reached, then ptr </li></ul><ul><li>holds the address of null character. </li></ul><ul><li>So, </li></ul><ul><li>int length=ptr-str; </li></ul>
23. 23. Pointers to Structures <ul><ul><li>struct part { float price ; char name [10] ; </li></ul></ul><ul><ul><li>} ; </li></ul></ul><ul><ul><li>struct part *p , thing; </li></ul></ul><ul><ul><li>p = &thing; </li></ul></ul><ul><ul><li>/* The following three statements are equivalent */ </li></ul></ul><ul><ul><li>thing.price = 50; </li></ul></ul><ul><ul><li>(*p).price = 50; /* () around *p is needed */ </li></ul></ul><ul><ul><li>p -> price = 50; </li></ul></ul>
24. 24. Pointer as Structure Member <ul><li>struct node{ </li></ul><ul><li>int data; </li></ul><ul><li>struct node *next; </li></ul><ul><li>}; </li></ul><ul><li>struct node a,b,c; </li></ul><ul><li>a.next = &b; </li></ul><ul><li>b.next = &c; </li></ul><ul><li>c.next = NULL; </li></ul>NULL a b c a.data = 1; a.next->data = 2; /* b.data =2 */ a.next->next->data = 3; /* c.data = 3 */ c.next = (struct node *) malloc(sizeof(struct node)); ……
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.