100 likes | 268 Views
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.
E N D
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 directly references a variable whose value is 5 5 sumPtr indirectly references a variable whose value is 5 sum sumPtr sum 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 *aPtr; a = 200; aPtr = &a; printf(“The address of a is %p \n”, &a); printf(“The value of aPtr is %p \n”, aPtr); printf(“The value of a is %d \n”, a); printf(“The value of *aPtr is %d \n”, *aPtr); }
Calling functions by reference #include <stdio.h> void cubebyref( int *); int main() { int num = 5; printf(“The number is %d \n”,num); cubeByRef(&num); printf(“The new value of number is %d \n”,num); return 0; } void cubeByRef(int *numPtr) { *numPtr = *nPtr * *nPtr * *nPtr ; }
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 bPtr have been declared. Since the array name is a pointer to the first element of the array: bPtr = b; • This is equivalent to bPtr = &b[0]; • Array element b[1] can be referenced by: *( bPtr + 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. • bPtr[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 *bPtr = 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(“bPtr[ %d ] = %d \n”, i, bPtr [i ]); printf(“pointer/offset notation \n”); for (offset = 0; offset < 4 ; offset ++) printf(“ *(bPtr + %d) = %d \n”, offset,*(bPtr + offset)); return 0; }
#include <stdio.h> void copy( char *, const char *); int main() { char string1[10], *string2 = “CS230”; char string3[10], string4[ ] = “Lab”; copy(string1, string2); printf(“string1 = %s \n”,string1); copy(string3,string4); printf(“string3 = %s \n”,string3); return 0; }
void copy(char *s1, const char *s2) { int i = 0; do { s1[i] = s2[i]; i++; } while (s2[i] != ‘\0’); s1[i] = ‘\0’; }