Pointers Pointers and Address Pointer variables Normal variables
Pointers
Pointers and Address • Pointer variables – Normal variables contain a specific value (direct reference). ex: count – Pointers contain address of a variable that has a specific value (indirect reference). Ex: count. Ptr count 7
Pointers and Address • Pointer declarations – * used with pointer variables int *my. Ptr; (pointer to an int ) – Multiple pointers require using a * before each variable declaration int *my. Ptr 1, *my. Ptr 2; – Initialize pointers to 0, NULL, or an address • 0 or NULL – points to nothing (NULL preferred)
Pointers and Address • Two important operators(& and *) • & (address operator) – returns address of the variable int y = 5; int *y. Ptr; y. Ptr = &y; // y. Ptr gets address of y y. Ptr “points to” y y 600000 5 yptr 500000 600000
Pointers and Address • * (indirection/dereferencing operator) – Returns a synonym/alias of what its operand points to – *yptr returns y (because yptr = &y) – * can be used for assignment • *yptr = 7 (the same as y = 7) • * and & are inverses – *&y y, &*yptr
Pointer and Function Arguments • Call by value void swap(int x, int y) { int t; t=x; x = y; y= t; } a = 10, b = 20 swap(a, b) swap main a b t 10 20 copy x 10 y 20 swap can’t work correctly
• Call by reference void swap(int *x, int *y) { int t; t=*x; *x = *y; *y= t; } a = 10, b = 20 swap(&a, &b) swap main a b 10 copy the address 20 *x the alias of a *y the alias of b t x &a y &b
Pointer Arithmetic • Arithmetic operations can be performed on pointers – Increment/decrement pointer (++ or --) • [note] ++/-- has higher precedence than * • (*i)++ != *i++ • *i++ *i, i = i + 1 (*i)++ increase the *i – Add an integer to a pointer( + or += , - or -=) – Pointer comparison ( <, == , > ) – See which pointer points to the higher numbered array element – Also, see if a pointer points to 0(NULL) • if (ptr == NULL)
Pointer Arithmetic • Pointers of the same type can be assigned to each other – If not the same type, a cast operator must be used • int *a = (int *)b, where b is a type of void * – Exception: pointer to void (type void *) • Generic pointer, represents any type • No casting needed to convert a pointer to void pointer • void pointers cannot be dereferenced
Pointers and Arrays • Arrays and pointers closely related – Array name like a constant pointer • int v[5] (v can be seen as int *) • int *v. Ptr = v; (v. Ptr = &v[0]) – Pointers can do array subscripting operations • v[i] == v. Ptr[i] == *(v+i) == *(v. Ptr+i) location 3000 3004 v[0] pointer variable v. Ptr v[1] 3008 v[2] 3012 v[3] 3016 v[4]
Pointers and Arrays – The index can be negative number. int a[5] = {4, 3, 2, 1, 0}; int *pt = a + 1; // The same as pt = &a[1] printf("%dn", pt[-1]); The output is 4
Character pointers and Functions • A string is a character array and it is terminated with the null character ‘ ’ • Two kinds of declaration of string – char a. String[] = “test” – Char *p. String = “test” a. Sting Test p. Sting Test
Case Study: String copy • Strcpy: copy string t to string s void strcpy(char *s, char *t) { int i=0; while((s[i] == t[i]) != ‘ ’) i++; }
Case Study: String copy • Using pointer to perform this task void strcpy(char *s, char *t) { while((*s = *t) != ‘ ’) { s++; t++; } }
Case Study: String copy • Because ++ has higher precedence, we can rewrite as following: void strcpy(char *s, char *t) { while((*s++ = *t++) != ‘ ’) ; } • ‘ ’ is equal to the integer 0 void strcpy(char *s, char *t) { while (*s++ = *t++) ; }
Array of pointers • Arrays can contain pointers • Example: array of strings – char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’ ’ suit[1] ’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ suit[2] ’C’ ’l’ ’u’ ’b’ ’s’ ’ ’ suit[3] ’S’ ’p’ ’a’ ’d’ ’e’ ’s’ ’ ’ • suit[i] is an pointer, suit[i][j] is a char – suit[1][1] == ‘i’ ’s’ ’ ’
Array of pointers • Simple example: command-line arguments – int main(int argc, char*argv[]) • argc: number of arguments • argv: a string array contains the arguments – arguments are separated by blanks. – argv[0] is the name by which the program invoked. • dir /w argv[0] == “dir”, argv[1] == “/w”
Array of pointers main(int argc, char *args[]) { int i; for(i=1; I < argc; i++) printf(“%s%s”, argv[i], (i < (argc -1))? ” “: “”); printf(“n”); } At command line: C: > echo Hello, world. C: >
Pointers to Functions • Pointer to function – Contains address of function – Similar to how array name is address of first element – Function name is starting address of code that defines function • Function pointers can be – Passed to functions – Stored in arrays – Assigned to other function pointers
Pointers to Functions • Declaration – Declare directly • int (*foo)(char, int *) means that pointer to a function which takes a char and an int * as arguments and returns an int • [nore] compare with int *foo(char int *) – Embedded in function declaration • int foo(char a, int *b) foo is a type of int (*foo)(char, int *)
Pointers to Functions • Call via function pointer: – int (*foo)(char, int *) foo is a pointer to function, *foo is the function (*foo)(char 1, i) is used to call the function int add(int a, int b) { return(a+b); } main() { int (*foo)(int, int); foo = add; printf("%dn", (*foo)(4, 5)); } The output will be: 9
Pointers to Functions • An example: qsort – in stdlib. h – protype: void qsort(void *base, size_t nmemb, size_t size, int(*compare)(const void *, const void *)); • • base : pointer of the array nmemb : array size : size of each element in array compare : a compare function
Pointers to Functions • Just define compare function for your purpose, you can sort anything. – For example, compare function for string, float, and integer.
- Slides: 23