120 likes | 142 Views
Pointers. Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address of a variable that refers to a specific value. A variable directly references a value. A pointer indirectly references a value.
E N D
Pointers • Pointers are variables that contain memory addresses as their values. • A variable name refers to a specific value. • A pointer contains an address of a variable that refers to a specific value. • A variable directly references a value. • A pointer indirectly references a value. • Referencing a value through a pointer is called indirection.
Operators • & ‘address of’ returns the address of an object or variable • * ‘value of’ refers to the value at a given address
int Sum = 5; int X = 3; int* z = &X; Directly and Indirectly referencing a variable z=100100, *z=3, &Sum=100011
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 should 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 = *numPtr * *numPtr * *numPtr ; }
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 : b, &b[0] • 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: int* bPtr = b; is equivalent to int* bPtr = &b[0]; • Array element b[1] can be referenced by: *( bPtr + 1) • 1 is the ‘offset’ with respect to the pointer. • This 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’; }