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

ics212-16-pointers3

 ics212-16-pointers3

Avatar for William Albritton

William Albritton

September 17, 2015
Tweet

More Decks by William Albritton

Other Decks in Programming

Transcript

  1. Arrays of Pointers  Common to have an array of

    strings  Which is an array of pointers  Where each array element points to a string  In other words, each array element contains the address of the 0th character of the array of characters  See example program at: arraypointer.c
  2. Initialization  Initialize an array of strings  Syntax: char

    *array[SIZE]={"string1", "string2", ...} char array[SIZE] = {“zero”, “one”, “two”, “three”, “four”};
  3. Access a String or Character?  To access a string,

    use one array subscript  Syntax: char *string = array[i];  To access a character, use two array subscripts  Syntax: char letter = array[i][j]; char *string2 = array[2]; //“two” char letter2 = array[2][0]; // ‘t’
  4. Program Crash!  The strings (array of characters) are stored

    in read-only memory (either in the code or data segment), so we cannot change the values of the characters, or it will crash your program //Segmentation fault (core dumped) array[0][0]=‘X’;
  5. Array of Pointers to Strings  An array of pointers

    to strings on the runtime stack name address value string array[4] 0xffbffa40 "four" array[3] 0xffbffa3c "three" array[2] 0xffbffa38 "two" array[1] 0xffbffa34 "one" array[0] 0xffbffa30 "zero" 0x109d8 0x109d0 0x109c8 0x109c0 0x109b8
  6. Strings: Arrays of Characters  The topmost string in read-only

    memory name address value array[4][4] 0x109dc array[4][3] 0x109db array[4][2] 0x109da array[4][1] 0x109d9 array[4][0] 0x109d8 '\0' 'r' 'u' 'o' 'f'
  7. Array of Characters on Stack  The bottommost string in

    read-only memory name address value array[0][4] 0x109bc array[0][3] 0x109bb array[0][2] 0x109ba array[0][1] 0x109b9 array[0][0] 0x109b8 '\0' 'o' 'r' 'e' 'z'
  8. Commandline Arguments  C programs can access the commandline arguments

     The words that you type on the UNIX commandline  The commandline arguments are stored in your program as an array of strings  Each space on the commandline separates each string  See example program: command.c
  9. Function main()  argc - argument count is the number

    of commandline arguments  argv - argument vector is a pointer to an array of character strings that contain the commandline arguments (also written as **argv) int main(int argc, char *argv[])
  10. Loop through the Array of Strings  You can use

    argc and argv[] in your program to access the commandline arguments for(i=argc-1;i>=0;i--){ printf("argv[%i] = %s\n", i, argv[i]); }
  11. Example Input and Output  Example commandline arguments on UNIX

    prompt:  Example program output: %./program one two three argv[3] = "three" argv[2] = "two" argv[1] = "one" argv[0] = "./program"
  12. Commandline Character Restrictions  Some characters have special meaning on

    the commandline, so you might get unexpected results  An apostrophe (') or double quotes (") are ignored  An asterisk (*) means all matching names, so it will display all the files and directories  Greater than (>) or less than (<) is for redirection
  13. No Program Crash  The strings (array of characters) for

    commandline arguments are stored on the runtime stack, so we can change the values of the characters, if needed argv[0][0]=‘X’;//no program crash printf(“%s”, argv[0]); // X/program
  14. Array of Pointers on Stack  An array of pointers

    to strings on the runtime stack name address value string argv[4] 0xffbffaa4 "four" argv[3] 0xffbffaa0 "three" argv[2] 0xffbffa9c "two" argv[1] 0xffbffa98 "one" argv[0] 0xffbffa94 "./program" 0xffbffbb8 0xffbffbb2 0xffbffbae 0xffbffbaa 0xffbffba0
  15. Strings: Arrays of Characters  The topmost string on runtime

    stack is the last arguments entered on the commandline name address value array[4][4] 0xffbffbbc array[4][3] 0xffbffbbb array[4][2] 0xffbffbba array[4][1] 0xffbffbb9 array[4][0] 0xffbffbb8 '\0' 'r' 'u' 'o' 'f'
  16. 'o' 'r' 'p' '/' '.' Array of Characters on Stack

     The bottommost string on runtime stack is the executable program file’s name name address value argv[0][4] 0xffbffba4 argv[0][3] 0xffbffba3 argv[0][2] 0xffbffba2 argv[0][1] 0xffbffba1 argv[0][0] 0xffbffba0
  17. Pointers to Functions  A pointer can also store the

    address of a function  A function name is the address in memory of the start of the function  Function pointers can be:  Passed to a function  Returned to functions  Stored in arrays  Assigned to other function pointers
  18. Array of Pointers to Functions  See example program at:

    functionpointer.c  Syntax to declare and initialize an array of functions:  returnType (*array[SIZE](listOfParameterTypes) = {function1, function2, function3, …}  Note that the return type, parameter types and parameter count have to match for each function  This is because arrays store data of the same type
  19. Declare and Initialize  Example code that initializes an array

    of three pointers to three functions with no arguments and return type void void (*array[SIZE])(void) = {function1,function2,function3};
  20. Function Definitions  Code for the three function definitions void

    function1(void){ printf("1st ");} void function2(void){ printf("2nd ");} void function3(void){ printf("3rd ");}
  21. Function Call  Function call syntax:  array[index](arguments); //if returns

    void  variable = array[index](arguments);  Example code: for (i=0;i<SIZE;i++){ array[i](); //1st 2nd 3rd }
  22. Array of Pointers to Function Definitions  An array of

    pointers on the runtime stack in the stack segment with address to function definitions, which are stored in the code segment name address value function definition array[2] 0xffbffa30 void function1(){printf("1st");} array[1] 0xffbffa2c void function1(){printf("2nd");} array[0] 0xffbffa28 void function1(){printf("3rd");} 0x10814 0x1080c 0x107ec
  23. Menu Example  An example of arrays of pointers to

    functions are often used in menu-driven programs  See example program: menu.c
  24. Declare and Initialize  Declares and initializes an array of

    3 pointers to 3 functions with a char argument & return type char char (*array[SIZE])(char) = {add1,add2,add3};
  25. Function Definitions  Code for the three function definitions char

    add1(char letter){ return letter+1;} char add2(char letter){ return letter+2;} char add3(char letter){ return letter+3;}
  26. Function Call  Function call syntax:  array[index](arguments); //if returns

    void  variable = array[index](arguments);  Example code: result = array[index](letter);
  27. Function Pointers as Parameters  A function can have parameters

    that are pointers to other functions  See example program at: sorting.c  Insertion sort with commandline input  Functions for ascending or descending order are passed as parameters to the insertion sort function  Note the error checking at the beginning of the program  We don’t want our programs to crash!
  28. Function Prototype  Function prototype syntax:  returnType1 function(returnType2 (*)(dataType1,

    dataType2, …), …);  Code example: void insertionsort(char *[],const int, const int, int (*)(char,char));
  29. Function Call  Function call syntax:  function(function1, function2, …);

    //if return type is void  variable = function(function1, function2, …);
  30. Function Call  The user chooses ascending or descending order

    if('a' == argv[1][1]){ insertionsort(argv, INDEX_OF_1ST_DIGIT, argc-1,ascend); } else{insertionsort(argv, INDEX_OF_1ST_DIGIT,argc-1,descend);}
  31. Function Definition Syntax  Function definition syntax for function parameters:

     returnType function(returnType2 (*function1)(dataType1, dataType2, …), …){ // function code with function calls function1(parameters); //if return type void dataType variable = function1(parameters); }
  32. Function Definition Example  Parameter compare() is ascend() or descend()

    void insertionsort(char *array[], const int startindex, const int endindex,int (*compare)(char,char)){ //code while ((j > startindex) && (compare(array[j - 1][0], temp))){ //code }
  33. Function Definitions  The functions that are used as parameters

    int ascend(char first, char second){ return first > second; } int descend(char first,char second){ return first < second; }