(16) Pointers-Compatibility-Mode

(16) Pointers-Compatibility-Mode - 10/19/2008 Swap Two...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
10/19/2008 1 They went that away! Pointers in C Nathan Friedman Fall, 2008 Swap Two Values void swap(int x, int y) { int temp; temp = x; 2008 Pointers 2 x = y; y = temp; } What Happens? void main () { int a, b; a = 27; 2008 Pointers 3 b = 103; swap(a,b); printf ("%d %d \n", a, b); } Surprise! >swap 27 103 2008 Pointers 4 Surprise! >swap 27 103 2008 Pointers 5 We wanted to see: 103 27 Surprise! >swap 27 103 2008 Pointers 6 We wanted to see: 103 27 What happened here?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10/19/2008 2 Surprise! >swap 27 103 We wanted to see 2008 Pointers 7 We wanted to see: 103 27 What happened here? This worked in Fortran! Why not in C? Parameter Passing ± It turns out that Fortran and C handle parameters very differently ± In C all parameters are passed by value 2008 Pointers 8 ± The parameters are treated as new local variables that are initialized to the argument values ± Any changes made are local and do not effect the arguments Fortran ? ± Remember how arguments were passed in Fortran? ± Expressions or constants had their values 2008 Pointers 9 Expressions or constants had their values put in new local variables representing the parameters ( call by value ) ± If the argument was a variable the parameter was treated as an alias for that variable ( call by reference ) C ? ± C is more uniform ± It treats all arguments the same way, the way Fortran treats expressions 2008 Pointers 10 C ? ± C is more uniform ± It treats all arguments the same way, the way Fortran treats expressions 2008 Pointers 11 ± But … That means that we have problems with functions like swap where we want the argument values to change What’s the solution? ± C allows us to manipulate addresses, called pointers ± If we pass a pointer as an argument, the 2008 Pointers 12 If we pass a pointer as an argument, the value of the argument doesn’t change
Background image of page 2
10/19/2008 3 What’s the solution? ± C allows us to manipulate addresses, called pointers ± If we pass a pointer as an argument, the 2008 Pointers 13 value of the argument doesn’t change ± But . . . The value in the cell pointed to could change Swapping Values in C void swap(int *px, int *py) { int temp; t* 2008 Pointers 14 temp = *px; *px = *py; *py = temp; } Let’s Compare void swap(int x, int y) { int temp; temp = x; void swap(int *px, int *py) 2008 Pointers 15 x = y; y = temp; } { int temp; temp = *px; *px = *py; *py = temp; } What is a Pointer? A pointer is a variable which contains the memory address of another variable. 2008 Pointers 16 Declaring a Pointer To declare a pointer to an integer variable: int *ip; 2008 Pointers 17 int ip; The variable ip will be able to store the address of an integer cell In general: type *name; Type We can have a pointer to any variable type (cell “shape”) Once we declare it, the pointer can only 2008 Pointers 18 be associated with a variable of the specific type we declared
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10/19/2008 4 How do we find the value pointed to?
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/15/2010 for the course COMP COMP 206 taught by Professor Vybihal during the Spring '04 term at McGill.

Page1 / 9

(16) Pointers-Compatibility-Mode - 10/19/2008 Swap Two...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online