1 / 24

EEE 243B Applied Computer Programming

EEE 243B Applied Computer Programming. Pointers and arrays practice. Practice. Write an include instruction that has stddef.h in it Declare a function prototype named IntArray that returns a pointer to int and that takes three pointers to int as parameters (px,py and pz)

river
Download Presentation

EEE 243B Applied Computer Programming

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. EEE 243BApplied Computer Programming Pointers and arrays practice

  2. Practice • Write an include instruction that has stddef.h in it • Declare a function prototype named IntArray that returns a pointer to int and that takes three pointers to int as parameters (px,py and pz) • Start a main() function returning void and having no parameters Maj JGA Beaulieu & Capt MWP LeSauvage

  3. Practice #include <stddef.h> int *IntArray(int *px, int *py, int *pz); void main(void) { }//main Maj JGA Beaulieu & Capt MWP LeSauvage

  4. Practice • Inside the main function: • Declare two integers, named firstInt (value of 5) and secondInt (value of 10) • Declare a pointer to int named pInt (initialize all pointers to NULL) • Declare a pointer to pointer to int named ppInt • Declare an array of int size 2 named numbers • Initialize this array to all zeros Maj JGA Beaulieu & Capt MWP LeSauvage

  5. Practice #include <stddef.h> int *IntArray(int *px, int *py, int *pz); void main(void) { int firstInt = 5; int secondInt = 10; int *pInt = NULL; int **ppInt = NULL; int numbers[2] = {0,0}; } //main Maj JGA Beaulieu & Capt MWP LeSauvage

  6. Practice • Inside the main function: • Call the function IntArray in your code so that the return value of the function is put into the pointer variable pInt • Pass the parameters to IntArray in the following order: • Address of firstInt, address of secondInt and address of the numbers array • Make ppInt point to pInt Maj JGA Beaulieu & Capt MWP LeSauvage

  7. Practice #include <stddef.h> int *IntArray(int *px, int *py, int *pz); void main(void) { int firstInt = 5; int secondInt = 10; int *pInt = NULL; int **ppInt = NULL; int numbers[2] = {0,0}; pInt = IntArray(&firstInt,&secondInt,numbers); ppInt = &pInt; } //main Maj JGA Beaulieu & Capt MWP LeSauvage

  8. Practice • Define the IntArray function to do the following: • Declare a pointer to int,pInt and make it point to the third parameter in the list (the array) • Put the first integer in the first element of the array • Post increment your pointer • Put the second parameter in the second element of the array • Return the address of the array (remember where you are pointing after the post increment) Maj JGA Beaulieu & Capt MWP LeSauvage

  9. Practice int *IntArray(int *px, int *py, int *pz) { int *pInt = pz; //pz is an address *pInt++ = *px; //note ++ is higher priority than * *pInt-- = *py; //pInt was pointing to numbers[1] return pInt; //Now pInt points to numbers[0] } //IntArray Maj JGA Beaulieu & Capt MWP LeSauvage

  10. #include <stddef.h> int *IntArray(int *px, int *py, int *pz); void main(void) { int firstInt = 5; int secondInt = 10; int *pInt = NULL; int **ppInt = NULL; int numbers[2] = {0,0}; printf("Value of first int in array: %d\n",numbers[0]); //print initial values printf("Value of second int in array: %d\n",numbers[1]); pInt = IntArray(&firstInt,&secondInt,numbers); //function call ppInt = &pInt; printf("Value of first int in array: %d\n",numbers[0]); //print value with index printf("Value of second int in array: %d\n",numbers[1]); printf("Value of first int in array: %d\n",*pInt++); //print with pointer printf("Value of second int in array: %d",**ppInt); //print with p-to-r to int getchar(); //wait } //main int *IntArray(int *px, int *py, int *pz) { int *pInt = pz; //pz is an address *pInt++ = *px; //note ++ is higher priority than * *pInt-- = *py; //pInt was pointing to numbers[1] return pInt; } //IntArray

  11. Practice • Soon we will be using the command line arguments capabilities of C • Remember that main() is a function like any other function • It is this function that is the root of your program • When you declare your main() function you normally do the following: void main (int argc, char **argv) Maj JGA Beaulieu & Capt MWP LeSauvage

  12. Practice • Remember that you can access the command line arguments (argv) one at a time using the array notation: argv[0], argv[1], … argv[argc -1] • argc is the count of the number of elements in the array • argv[0] is the name of the program Maj JGA Beaulieu & Capt MWP LeSauvage

  13. Practice • You are experienced with the dll.exe program used to download your programs into the Roverbot. • Write a main() function for dll.c so that you can separate the tockens from the command line • You are to recognize the following two options –t with value usb and –r [0..15] • If the command line argument for –t option is not usb or that –r is not in the range, an error must be generated Maj JGA Beaulieu & Capt MWP LeSauvage

  14. Practice • Include stdio.h, stdlib.h and string.h in your header • You will be using the atoi function that can convert alphanumeric characters to ints: int atoi(const char *real_num); • You will also be using strcmp to compare the tockens you extract with constants in your code: int strcmp(const char *str1, const char *str2); • Define your main() Maj JGA Beaulieu & Capt MWP LeSauvage

  15. Practice #include <stdlib.h> #include <string.h> #include <stdio.h> void main(int argc, char **argv) { }//main Maj JGA Beaulieu & Capt MWP LeSauvage

  16. Practice • Declare two int variables, one to count the number of arguments (numArgs) and the other to store the Roverbot number (bot) if the –r option is selected • Write an if statement that verifies that you have more than 2 arguments (argc) • If not, print an error message (printf) and exit (exit(1)) • Write a loop that iterates through the arguments (tokens) Maj JGA Beaulieu & Capt MWP LeSauvage

  17. Practice #include <stdlib.h> #include <string.h> #include <stdio.h> void main(int argc, char **argv) { int numArgs = 1; int bot = 0; if (argc <= 2) { printf("Need more arguments"); exit(1); } while (numArgs < argc) { }//while } Maj JGA Beaulieu & Capt MWP LeSauvage

  18. Practice • A parser, like the dll.exe program, iterates through the command line arguments (**argv) and checks for the validity of options and their values • Write your first token parsing statement: • With the if statement, check to see if the command line argument (argv[numArgs]) is the –t option • For this you will need the strcmp() function • If it is, check if the next token is the usb value • If it is, then print some statement to this effect • If it is not then get out with an error message about the value of the tty not being valid Maj JGA Beaulieu & Capt MWP LeSauvage

  19. Practice … while (numArgs < argc) { if (!strcmp(argv[numArgs],"-t")) { //get the kind of tty numArgs++; if (!strcmp(argv[numArgs],"usb")) printf("We have -t and usb\n"); else { printf("ERROR - Invalid tty"); exit(1); }//else }//if -t }//while … Maj JGA Beaulieu & Capt MWP LeSauvage

  20. Practice • Write your second token parsing statement: • With the else-if statement, check to see if the next command line argument (argv[numArgs]) is the –r option • If it is, check if the valueof the next token is within 0..15 range. • You will need the atoi()function to transform the string into int • If the value is good, then print some statement to this effect • If it is not then get out with an error message stating that the value of the bot was not in range Maj JGA Beaulieu & Capt MWP LeSauvage

  21. Practice … else if (!strcmp(argv[numArgs],"-r")) { numArgs++; bot = atoi(argv[numArgs]); if (bot >= 0 && bot <= 15) printf("We have -r with bot# %d\n", bot); else { printf("ERROR - Invalid bot number"); exit(1); } }//if -r … Maj JGA Beaulieu & Capt MWP LeSauvage

  22. Practice • If the token does not match any of the options that you want, give an error message (not –t or –r) • Do not forget to update your condition (numArgs) close your loop • Voila!! Your first parser Maj JGA Beaulieu & Capt MWP LeSauvage

  23. Practice }//if -r else { printf("ERROR - Invalid argument"); exit(1); }//invalid arg numArgs++; }//while exit(0); }//main Maj JGA Beaulieu & Capt MWP LeSauvage

  24. #include <stdlib.h> #include <string.h> #include <stdio.h> void main(int argc, char **argv) { int numArgs = 1; int bot = 0; if (argc <= 2) { printf("Need more arguments"); exit(1); } while (numArgs < argc) { if (!strcmp(argv[numArgs],"-t")) { //get the kind of tty numArgs++; if (!strcmp(argv[numArgs],"usb")) printf("We have -t and usb\n"); else { printf("ERROR - Invalid tty"); exit(1); }//else }//if -t else if (!strcmp(argv[numArgs],"-r")) { numArgs++; bot = atoi(argv[numArgs]); if (bot >= 0 && bot <= 15) printf("We have -r with bot# %d\n", bot); else { printf("ERROR - Invalid bot number"); exit(1); } }//if -r else { printf("ERROR - Invalid argument"); exit(1); }//invalid arg numArgs++; }//while exit(0); }//main

More Related