Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-15-pointers2

 ics212-15-pointers2

Avatar for William Albritton

William Albritton

September 17, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Memory Allocation  Generic pointers  Call by value 

    Call by reference  Pointers and arrays
  2. Generic Pointer (Void Pointer)  When a pointer is assigned

    to another pointer, they must be of the same type (or use a cast operator)  To make code flexible, use a pointer of type void  Can be assigned to any pointer type  Any pointer type can be assigned to it  Cannot be dereferenced (must cast it first)
  3. Dereferencing Void Pointers  The compiler must know the data

    type to know how to dereference (*) a pointer  For integer pointers, the compiler knows that it refers to 4-bytes in memory, so it can get its value  Since a void pointer can be any data type, the compiler cannot dereference it (without a cast)
  4. Void Pointer Syntax  To initialize a void pointer, used

    data type void and an asterisk (*)  void *genericPointer = NULL;  You can assign a void pointer an address just like any “normal” pointer  genericPointer = &variableName;
  5. Void Pointer Syntax  To dereference a void pointer, you

    need to cast it to the appropriate data type, and add an asterisk (*) to the cast operator, and then dereference (*) it, as you would any “normal” pointer  variableName = *((dataType *) voidPointer);  Note that the inner parenthesis is the cast operator, and the outer parenthesis is regular parenthesis
  6. Variable Declarations  Local variables on the runtime stack name

    value address number1 0xffbffa34 letter1 0xffbffa33 genericPointer 0xffbffa2c int number1 = 5; //int is 4 bytes char letter1 = ‘A’; //char is 1 byte void *genericPointer= NULL; 5 ‘A’ NULL
  7. Assign an Address to a Generic Pointer  Local variables

    on the runtime stack name value address number1 0xffbffa34 letter1 0xffbffa33 genericPointer 0xffbffa2c //point genericPointer to number1 genericPointer = &number1; 5 ‘A’ 0xffbffa34
  8. Change a Value with a Generic Pointer  Cast (dataType*),

    then dereference to access value name value address number1 0xffbffa34 letter1 0xffbffa33 genericPointer 0xffbffa2c *((int *)genericPointer) = *((int *)genericPointer) * 13; 65 ‘A’ 0xffbffa34
  9. Assign Another Address to Pointer  A generic pointer points

    to variable of any data type name value address number1 0xffbffa34 letter1 0xffbffa33 genericPointer 0xffbffa2c //point genericPointer to letter1 genericPointer = &letter1; 65 ‘A’ 0xffbffa33
  10. Change a Value with a Generic Pointer  A generic

    pointer points to variable of any data type name value address number1 0xffbffa34 letter1 0xffbffa33 genericPointer 0xffbffa2c *((char *)genericPointer) = *((char *)genericPointer) + 13; 65 'N' 0xffbffa33
  11. Call by Value  Changing the values of the formal

    parameters in the function does NOT change the values of the actual parameters (arguments)  Makes a copy of the actual parameters, so changes to the formal parameters in the function do not change the values of the actual parameters  See example program: swap.c
  12. Initialize Variables and Call Function  Runtime stack before swap1()

    function call name value address letter1 0xffbffa37 letter2 0xffbffa36 char letter1 = ‘A’; char letter2 = ‘Z’; swap1(letter1,letter2); 'A' 'Z'
  13. Function Definition  Call by value – makes a copy

    of each parameter void swap1(char letter3, char letter4){ char temp = letter3; letter3 = letter4; letter4 = temp; }
  14. Start of Function  Runtime stack at start of swap1()

    function definition  Adds formal parameters to stack from right to left, so that is why letter4 is listed before letter3 name value address letter1 0xffbffa37 letter2 0xffbffa36 letter4 0xffbffa10 letter3 0xffbffa0c 'A' 'Z' ‘Z' ‘A'
  15. End of Function  Runtime stack at end of swap1()

    function definition  Actual parameter values do NOT change name value address letter1 0xffbffa37 letter2 0xffbffa36 letter4 0xffbffa10 letter3 0xffbffa0c temp 0xffbff9b7 'A' 'Z' ‘A' ‘Z' ‘A'
  16. Call by Reference  Changing the values of the formal

    parameters in the function DOES change the values of the actual parameters (arguments)  Makes a copy of the address of the actual parameters, so changes to the dereferenced (*) formal parameters in the function DO change the values of the actual parameters
  17. 'A' 'Z' Initialize Variables and Call Function  Runtime stack

    before swap2() function call  The address of each variable is the actual parameter name value address letter1 0xffbffa37 letter2 0xffbffa36 char letter1 = ‘A’; char letter2 = ‘Z’; swap2(&letter1,&letter2);
  18. Function Definition  Call by reference – makes a copy

    of the address of each parameter void swap2(char *letter5, char *letter6){ char temp = *letter5; *letter5=*letter6; *letter6=temp; }
  19. Start of Function  Runtime stack at start of swap2()

    function definition  Adds formal parameters to stack from right to left, so that is why letter6 is listed before letter5 name value address letter1 0xffbffa37 letter2 0xffbffa36 letter6 0xffbffa10 letter5 0xffbffa0c 'A' 'Z' 0xffbffa36 0xffbffa37
  20. End of Function  Runtime stack at end of swap2()

    function definition  Actual parameter values DID change name value address letter1 0xffbffa37 letter2 0xffbffa36 letter6 0xffbffa10 letter5 0xffbffa0c temp 0xffbff9b7 ‘Z' ‘A' 0xffbffa36 0xffbffa37 ‘A'
  21. Initialize Variables and Call Function  We can also use

    pointers as the actual parameters in a call by reference function call, as the pointers are storing the addresses of the original variables char *pointer1 = &letter1; char *pointer2 = &letter2; swap2(pointer1,pointer2);
  22. Pointers & Arrays  In C, pointers & arrays are

    almost interchangeable  Because an array name is an address, an array name can be thought of as a constant pointer  Because a pointer stores an address, pointers can be used to do array subscripting  This is called “pointer/offset notation”
  23. Pointer/Offset Notation  Syntax for pointer/offset notation:  variable =

    *(array + index);  variable = *(pointer + index);  The index is added to the address of the first (0th) element in the array, which is at a lower address in memory, while the rest of the array elements are stored at higher address in memory
  24. Initialize Variables & Assign Address  See example program at:

    array.c int array[] = {10,20,30,40}; int *pointer = NULL; //assign array's address to pointer pointer = array; pointer = &array[0];
  25. Array Elements in Memory  Runtime stack with array elements

    and pointer to start of array, which is the first (0th) array element name value address array[3] 0xffbffa34 array[2] 0xffbffa30 array[1] 0xffbffa2c array[0] 0xffbffa28 pointer 0xffbffa24 40 30 20 10 0xffbffa28
  26. Four Ways to Access Array Elements  You can use

    the array name with array subscript notation, or pointer/offset notation //array subscript notation: printf(“%i\n”,array[0]); //pointer/offset notation: printf(“%i\n”,*(array+1));
  27. Four Ways to Access Array Elements  You can also

    use the pointer name with array subscript notation, or pointer/offset notation //array subscript notation: printf(“%i\n”,pointer[2]); //pointer/offset notation: printf(“%i\n”,*(pointer+3));
  28. Alternative Array Loop  You can also loop through an

    array with a pointer pointer = array; while(pointer != (array+4)){ printf(“%i\n”, *pointer); pointer++; }
  29. Character Pointers (Strings)  Using an array and a pointer

    to declare and initialize strings looks the same and outputs the same  But there are small different in how you use the strings later on in your program  This can make your program not compile or crash
  30. Array String  Array of characters + '\0'  Can

    change the characters in the array  Cannot change the array’s address  Cannot change the length of the array (string) char array[] = “character array”;
  31. Pointer String  Points to constant characters + '\0' 

    Cannot change the characters in the constant string, or your program will crash  Can change the pointer value char *pointer = “constant array”; pointer[0] = ‘Z’; //Segmentation fault pointer = array; //This is OK
  32. String Copy  Must be careful when copying strings 

    Should copy each character to new string  Will create two different strings  Should not simply assign a new address  Will have two pointers pointing to the same string
  33. Incorrect String Copy  This creates a pointer pointing to

    an array char array1[] = “string”; char *pointer1 = array1; pointer1[0] = ‘X’; printf(“%s\n”,array1); //Xtring printf(“%s\n”,pointer1); //Xtring
  34. Correct String Copy  This uses pointers to help make

    two separate arrays char array2[] = “string”; char array3[SIZE] = {‘\0’}; //end of string marker char *pointer2 = array2; char *pointer3 = array3;
  35. Correct String Copy  The while loop copies each character

    to array3 while(*pointer2 != ‘\0’){ *(pointer3++) = *(pointer2++); } array2[0]=‘X’; printf(“%s\n”,array2); //Xtring printf(“%s\n”,array3); //string
  36. Memory Allocation  Generic pointers  Call by value 

    Call by reference  Pointers and arrays