1 / 11

Pointers

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.

flagg
Télécharger la présentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  2. Operators • & ‘address of’ returns the address of an object or variable • * ‘value of’ refers to the value at a given address

  3. int Sum = 5; int X = 3; int* z = &X; Directly and Indirectly referencing a variable z=100100, *z=3, &Sum=100011

  4. 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.

  5. /* 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); }

  6. 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 ; }

  7. 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.

  8. 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.

  9. #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; }

  10. #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; }

  11. void copy(char *s1, const char *s2) { int i = 0; do { s1[i] = s2[i]; i++; } while (s2[i] != ‘\0’); s1[i] = ‘\0’; }

More Related