Pointers Pointers are variables that contain memory addresses
Pointers • Pointers are variables that contain memory addresses as their values. • A variable directly contains a specific value. • A pointer contains an address of a variable that contains a specific value. • A variable name directly references a value. • A pointer indirectly references a value. • Referencing a value through a pointer is called indirection.
Directly and Indirectly referencing a variable sum 5 sum. Ptr sum 5 sum directly references a variable whose value is 5 sum. Ptr indirectly references a variable whose value is 5
Initialization • A pointer may be initialized to 0 , NULL or an address. • NULL is a symbolic constant defined in <stdio. h> • A pointer with the value NULL points to nothing. • The only integer that can be assigned to a pointer is 0. Dereferencing • The * operator – indirection or dereferencing operator, returns the value of the object that its operand points to in memory. This is called dereferencing the pointer.
/* Program to display the address and value of a pointer */ #include <stdio. h> int main() { int a; int *a. Ptr; a = 200; a. Ptr = &a; printf(“The } address of a is %p n”, &a); value of a. Ptr is %p n”, a. Ptr); value of a is %d n”, a); value of *a. Ptr is %d n”, *a. Ptr);
Calling functions by reference #include <stdio. h> void cubebyref( int *); int main() { int num = 5; printf(“The number is %d n”, num); cube. By. Ref(&num); printf(“The new value of number is %d n”, num); return 0; } void cube. By. Ref(int *num. Ptr) { *num. Ptr = *n. Ptr * *n. Ptr } * *n. Ptr ;
Relationship between Pointers and Arrays • An array name is the address of the first element of an array. • int b[5]; • The address of the first element : using %p to print - b, &b[0], &b. • Assume that int b[5] and pointer variable b. Ptr have been declared. Since the array name is a pointer to the first element of the array: b. Ptr = b; • This is equivalent to b. Ptr = &b[0]; • Array element b[1] can be referenced by: *( b. Ptr + 1) • 1 is the offset to the pointer. • The preceding notation is called pointer / offset notation.
• In pointer/offset notation, the offset is the same as the array subscript. • Pointers can be subscripted exactly as arrays can. • b. Ptr[1] refers to the array element b[1]. • This is called pointer / subscript notation.
#include <stdio. h> int main() { int b[] = { 10 , 20 , 30 , 40 }; int *b. Ptr = b; int i, offset; printf(“Pointer/offset notation where the pointer is ”); printf(“the array name n”); for(offset = 0; offset < 4; offset++) printf(“*(b + %d) = %d n”, offset, *( b + offset) ); printf(“Pointer/subscript notation n”); for( i = 0 ; i < 4 ; i++) printf(“b. Ptr[ %d ] = %d n”, i, b. Ptr [i ]); printf(“pointer/offset notation n”); for (offset = 0; offset < 4 ; offset ++) printf(“ *(b. Ptr + %d) = %d n”, offset, *(b. Ptr + offset)); return 0; }
#include <stdio. h> void copy( char *, const char *); int main() { char string 1[10], *string 2 = “CS 230”; char string 3[10], string 4[ ] = “Lab”; copy(string 1, string 2); printf(“string 1 = %s n”, string 1); copy(string 3, string 4); printf(“string 3 = %s n”, string 3); return 0; }
void copy(char *s 1, const char *s 2) { int i = 0; do { s 1[i] = s 2[i]; i++; } while (s 2[i] != ‘ ’); s 1[i] = ‘ ’; }
- Slides: 10