Download presentation
Presentation is loading. Please wait.
Published byΕυθαλία Μανιάκης Modified over 6 years ago
1
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions
2
Command-line Arguments
The main( ) function can be called with arguments Must declare them to use them main (int argc, char *argv[ ]) The value of argc is the number of char strings in the array argv[ ] which is an array of ptrs to the command line tokens separated by white space Element argv[0] always points to the command name typed in by the user to invoke main If there are no other arguments, argc = 1
3
Command-line Arguments
If there are other arguments: For example, if the program was compiled as echo, and the user typed echo hello, world argc will be 3 (the number of strings in argv[]) argv[0] points to the beginning address of “echo” argv[1] points to the beginning address of “hello,” argv[2] points to the beginning address of “world”
4
Command-line Arguments
The program can print back the arguments typed in by the user following the echo command: int main (int argc, char *argv[ ]) { /* envision argc = 3, *argv[0]=“echo”, …*/ while (--argc > 0) printf("%s%s", *++argv, (argc > 0) ? " " : ""); printf("\n"); return 0; }
5
Parsing Arguments Nice example given in Section The program compiled with the run file name “find” looks for a pattern in standard input and prints out lines found. Want options -x and -n for invocation: find [-x] [-n] pattern OR find [-xn] pattern Program will parse option flags: except and number. Loop to parse options and set up flags is:
6
Parsing Arguments Loop through argv entries with first char == '-' (maybe find -x -n pattern) while (--argc > 0 && (*++argv)[0] == '-') while (c = *++argv[0]) switch (c) { case 'x' : except = 1; break; case 'n' : number = 1; break; default : printf("illegal option %c\n", c);break; }
7
Parsing Arguments How does (*++argv)[0] and *++argv[0] differ?
Increments pointer to argv[] array De-reference the pointer and get the resulting pointer to the string De-reference the pointer to the string and obtain the ascii value (‘h’) of the first character in the string *++argv[0] De-reference argv and obtain the pointer to the string Increment the resulting pointer and it points to the second position of the string De-reference the pointer to get the ascii value ‘c’
8
Difference Between (*++argv)[0] and *++argv[0]
‘h’ ‘o’ ‘’\0’’ argv[0] *++argv[0]=‘c’ argv argv[0] (*++argv) argv[1] ‘h’ ‘e’ ‘l’ ‘o’ ‘\0’ argv[2] (*++argv)[0]= ‘h’ ‘w’ ‘o’ ‘r’ ‘l’ ‘d’ ‘\0’
9
typedef typedef creates a new name for existing type
typedef int Boolean; typedef char *String; String ptr; Does not create a new type in any real sense No new semantics for the new type name! Variables declared using new type name have same properties as variables of original type
10
typedef Similar to how we allocated space for a LinkedList node:
typedef struct tnode { char *word; int count; treeptr left; treeptr right; } treenode; typedef struct tnode *treeptr; Similar to how we allocated space for a LinkedList node: treeptr talloc(void) { return (treeptr) malloc(sizeof(treenode)); } We can use treeptr instead of the pointer-to- the-struct type. struct tnode *talloc(void) { return (struct tnode *) malloc(sizeof(struct tnode));
11
typedef Used to provide clearer documentation: treeptr root; versus
struct tnode *root; Used to create machine independent variable types: typedef int size_t; /* size of types */ typedef int ptrdiff_t; /* difference of pointers */
12
Unions A Union is a variable that may hold objects of different types (at different times or for different instances of use) union u_tag { int ival; float fval; char *sval; } u;
13
Unions Address Value Variable 0x82ff6f80 *junk* ival, fval, sval 0x82ff6f81 0x82ff6f82 0x82ff6f83 0x82ff6f84 sval 0x82ff6f85 0x82ff6f86 0x82ff6f87 A union will be allocated enough space for the largest type in the list of possible types Same as a struct except all members have a zero offset from the base address of union u ival fval *sval
14
Unions The operations allowed on unions are the same as operations allowed on structs: Access a member of a union union u_tag x; x.ival = … ; Assign to union of the same type union u_tag y; x = y;
15
Unions(cont’d) Create a pointer to / take the address of a union
union u_tag x; union u_tag *px = &x; Access a member of a union via a pointer px->ival = … ;
16
Unions Program code must know which type of value has been stored in a union variable and process using correct member type DON’T store data in a union using one type and read it back via another type in attempt to get the value converted between types x.ival = 12; /* put an int in union */ float z = x.fval; /* don’t read as float! */
17
Bit-Fields Bit fields are used to get a field size other than 8 bits (char), 16 bits (short on some machines) or 32 bits (long on most machines) Allows us to “pack” data one or more bits at a time into a larger data item in memory to save space, e.g. 32 single bit fields to an int Can use bit fields instead of using masks, shifts, and or’s to manipulate groups of bits
18
Bit-Fields Uses struct form: struct {
unsigned int flg1 : 1; /* called a “bit field“ */ unsigned int flg2 : 1; /* ": 1" "1 bit in length " */ unsigned int flg3 : 2; /* ": 2" "2 bits in length" */ } flag; /* variable */ Field lengths must be an integral number of bits
19
Bit-Fields Access bit fields by member name same as any struct – just limited number of values flag.flg1 = 0; /* or = 1; */ flag.flg3 = 0; /* or = 1; = 2; =3; */
20
Bit-Fields Minimum size data item is implementation dependent, as is order of fields in memory Don’t make any assumptions about order!! Sample memory allocation: flags 2 1 1 flags.flg3 flags.flg2 flags.flg1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.