360 likes | 611 Views
B Smith: Required 2 lectures Fall04. Math 130 Introduction to Computing Functions and Introduction to Pointers Lecture # 10. Topics. Introduction to pointers Pointers as function parameters. ch:. ’A’. 0x2000. Memory Address of a Variable. char ch = ’A’;.
E N D
B Smith: Required 2 lectures Fall04 Math 130Introduction to ComputingFunctions and Introduction to PointersLecture # 10
Topics • Introduction to pointers • Pointers as function parameters
ch: ’A’ 0x2000 Memory Address of a Variable char ch = ’A’; The memory address of the variable ch The value of the variable ch
char ch = ’A’; ’A’ 0x2000 &ch yields the value 0x2000 The &Operator • Gives the memory address of an object • Also known as the “address operator”
“conversion specifier” for printing a memory address Example: char ch; printf(“%p”, &ch);
0x3A15 0x2000 chPtr Pointers A variable which can store the memory address of another variable 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc ‘A’ ch
Pointers • A pointer is a variable which… • Contains a memory address • Points to a specific data type • Pointer variables are usually named varPtr
Example: char* cPtr; cPtr: 0x2004 Can store an address of variables of type char • We say cPtris a pointer to a character
c: cPtr: A 0x2000 0x2004 Pointers and the&Operator Example: char c = ’A’; char *cPtr; cPtr = &c; Assigns the address ofc to cPtr 0x2000
We can have pointers to any data type Example: int* numPtr; float* xPtr; • The * can be anywhere between the type and the variable Example: int *numPtr; float * xPtr; Notes on Pointers
You can assign the address of a variable to a “compatible” pointer using the &operator int aNumber; int *numPtr; numPtr = &aNumber; Example: • You can print the address stored in a pointer using the %pconversion specifier Example: printf(“%p”, numPtr); Notes on Pointers (cont)
Notes on Pointers (cont) Beware of pointers which are not initialized! int *numPtr; ??? numPtr
Notes on Pointers (cont) • When declaring a pointer, it is a good idea to always initialize it toNULL (a special pointer constant) int *numPtr = NULL; NULL numPtr
The *Operator • Allows pointers to access variables they point to • Also known as “dereferencing operator” • Should not be confused with the * in the pointer declaration
Easy Steps to Pointers • Step 1: Declare the variable to be pointed to int num; char ch = ‘A’; float x; num: ch: ‘A’ x:
NULL NULL NULL Easy Steps to Pointers (cont) • Step 2: Declare the pointer variable int num; char ch = ‘A’; float x; numPtr: chPtr: int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; xPtr: num: ch: ‘A’ x:
Easy Steps to Pointers (cont) • Step 3: Assign address of variable to pointer int num; char ch = ‘A’; float x; numPtr: addr of num chPtr: addr of ch int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; xPtr: addr of x numPtr = # num: chPtr = &ch; xPtr = &x; ch: ‘A’ x: A pointer’s type has to correspond to the type of the variable it points to
Easy Steps to Pointers (cont) • Step 4: De-reference the pointers int num; char ch = ‘A’; float x; numPtr: addr of num chPtr: addr of ch int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; xPtr: addr of x numPtr = # chPtr = &ch; xPtr = &x; num: 65 ch: ‘A’ *xPtr = 0.25; *numPtr = *chPtr; x: 0.25
Your Turn… • Write a fragment of C code that does the following: • Declares 3 integer variables called a, b, and c. • Declares 3 integer pointers p1, p2, and p3. • Assigns the values 34, 10, and –4 to a, b, and c • Initializes p1 with the addres of a and initializes p2 with the address of b • Points p3 to the same item pointed to by p2 • Prints out the contents pointed to by p1, p2 and p3
Pointers and Function Parameters • Example: Function to swap the values of two variables swap x: 1 y: 2 x: 2 y: 1
Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; }
Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } x: 1 y: 2
Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } tmp: a: 1 b: 2 x: 1 y: 2
Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } 1 tmp: a: 1 b: 2 x: 1 y: 2
Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } tmp: 1 a: 2 b: 2 x: 1 y: 2
Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } tmp: 1 a: 2 b: 1 x: 1 y: 2
Bad swap #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; return; } int main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); return 0; } tmp: 1 a: 2 b: 1 x: 1 y: 2
Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; }
Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } x: 1 y: 2
Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } tmp: a: addr of x b: addr of y x: 1 y: 2
Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } tmp: 1 a: addr of x b: addr of y x: 1 y: 2
Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } tmp: 1 a: addr of x b: addr of y x: 2 y: 2
Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } 1 tmp: a: addr of x b: addr of y x: 2 y: 1
Good swap #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; return; } int main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); return 0; } x: 2 y: 1
scanfdemystified char ch; int numx; float numy; scanf(“%c %d %f”, &ch, &numx, &numy); Pointers and Function Arguments • Change the value of an actual parameter variable
Lessons • Pointers are simply variables that contain an address • a pointer holds an address • a pointer holds an address • a pointer holds an address