CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

C Language.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Lecture 20 Arrays and Strings
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Structures, Unions, and Typedefs CIS 1057 Fall Structures, Unions, and Typedefs CIS 1057 Computer Programming in C Fall 2013 (Many slides based on/borrowed.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Structures, Unions, and Typedefs CS-2301 D-term Structures, Unions, and Typedefs CS-2301 System Programming D-term 2009 (Slides include materials.
UBC104 Embedded Systems Variables, Structures & Pointers.
Starting out with C++1 Chapter 9 – Pointers Getting the address of a Variable Why do we have pointers? Indirection – difference between –Will you go out.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Structures and UnionsCS-2301 B-term Structures and Unions CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
8. Structures, File I/O, Recursion 18 th October IIT Kanpur 1C Course, Programming club, Fall 2008.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
1 Homework HW6 due class 22 K&R 6.6 K&R 5.7 – 5.9 (skipped earlier) Finishing up K&R Chapter 6.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
0 Chap. 5 Pointers and Arrays 5.3Pointers and Arrays 5.4Address Arithmetic 5.5Character Pointers and Functions 5.6Pointer Arrays; Pointers to Pointers.
Homework Finishing up K&R Chapter 6 today Also, K&R 5.7 – 5.9 (skipped earlier)
Lecture Starting K&R Chapter 7 and Appendix B Also, UNIX – Various chapters in Glass.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Week 10 - Wednesday.  What did we talk about last time?  Linked list implementations  Started doubly linked lists.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
1 CSC2100B Data Structure Tutorial 1 Online Judge and C.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Programming in C Arrays, Structs and Strings. 7/28/092 Arrays An array is a collection of individual data elements that is:An array is a collection of.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
1 Homework Continue with K&R Chapter 5 –Skipping sections for now –Not covering section 5.12 Continue on HW5.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
Object Oriented Programming Lecture 2: BallWorld.
Java Programming Language Lecture27- An Introduction.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Lecture 10 union, enumeration, bit
Lecture 5 array declaration and instantiation array reference
Chapter 5, Pointers and Arrays
Stack and Heap Memory Stack resident variables include:
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
The Machine Model Memory
A bit of C programming Lecture 3 Uli Raich.
C LANGUAGE MULTIPLE CHOICE QUESTION
Documentation Need to have documentation in all programs
Command Line Arguments
CSE 374 Programming Concepts & Tools
Instructor: Ioannis A. Vetsikas
DATA HANDLING.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
C Structures, Unions, Bit Manipulations and Enumerations
Chapter 15 Pointers, Dynamic Data, and Reference Types
Structures, Unions, and Typedefs
Lecture 2 SCOPE – Local and Global variables
EE 312 Exam I Review.
Appendix F C Programming Environment on UNIX Systems
Homework Continue with K&R Chapter 5 Skipping sections for now
Java Programming Language
Chapter 9: Pointers and String
Chapter 9: Pointers and String
C Programming - Lecture 5
C Language B. DHIVYA 17PCA140 II MCA.
EE 312 Exam I Review.
SPL – PS2 C++ Memory Handling.
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
EE 312 Exam I Review.
Presentation transcript:

CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions

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

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”

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

Parsing Arguments Nice example given in Section 5.10. 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:

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

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’

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’

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

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));

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 */

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;

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

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;

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 = … ;

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! */

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

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

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; */

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