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. 

Slides:



Advertisements
Similar presentations
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Advertisements

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.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
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.
C structures and unions (Reek, Ch. 10) 1CS 3090: Safety Critical Programming in C.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
Arrays and Pointers in C Alan L. Cox
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Pointers CSE 2451 Rong Shi.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
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.
What we will cover A crash course in the basics of C “Teach yourself C in 21 days”
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
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.
C is a high level language (HLL)
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Object Oriented Programming Lecture 2: BallWorld.
Java Programming Language Lecture27- An Introduction.
ME2008– W05 MID1- Reference 2016Q1- Source: Deitel /C- How To.
1 Binghamton University Exam 1 Review CS Binghamton University Birds eye view -- Topics Information Representation Bit-level manipulations Integer.
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
Intro to Pointers in C CSSE 332 Operating Systems
The Machine Model Memory
C Primer.
A bit of C programming Lecture 3 Uli Raich.
Chapter 10-1: Structure.
Characters and Strings
Command Line Arguments
Command line arguments
Command Line Arguments
CSE 303 Concepts and Tools for Software Development
C Structures, Unions, Bit Manipulations and Enumerations
C Basics.
Module 2 Arrays and strings – example programs.
Programming Languages and Paradigms
Basic notes on pointers in C
Command Line Arguments
C Structures, Unions, Bit Manipulations and Enumerations
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Structures and Union.
CS 240 – Lecture 18 Command-line Arguments, Typedef, Union, Bit Fields, Pointers to Functions.
Pointers, Dynamic Data, and Reference Types
Strings and Streams Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Lecture 8 Data structures
Structures, Unions, and Typedefs
Structures and Union.
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
C Structures, Unions, Bit Manipulations and Enumerations
EE 312 Exam I Review.
C Programming Pointers
Programming in C Pointers and Arrays.
Structures and Union.
Characters and Strings
15213 C Primer 17 September 2002.
EE 312 Exam I Review.
SPL – PS2 C++ Memory Handling.
Structures, Unions, and Enumerations
EE 312 Exam I Review.
Presentation transcript:

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. 

Two dimensional array vs pointer to array char *students[] = {"Fi" , "April", "Raghd", "Jack", "Bobby"}; students: FI\0 April\0 Raghd\0 Jack\0 Bobby\0 Total memory on 64 bit machine: 66 bytes char stu_arr[][10] = {"Fi" , "April", "Raghd", "Jack", "Bobby"}; stu_arr: Fi\0 April\0 Raghd\0 Jack\0 Bobby\0 10 20 30 40 Total memory on 64 bit machine: 50 bytes students[2][3]; and stu_arr[2][3]; are both valid

char *argv[] argv: Argument Vector Pointer to array of character strings argv[0]:name of current program Number of args at least 1 Argv[1- n]: command line arguments Terminated by a null pointer argv: // print out argv int main(int argc, char *argv[]){ while(*argv){ char *c = *argv++; while(*c){ putchar(*c++); } putchar('\n'); cat\0 hello\0 move\0 0/NULL

Function Pointers Functions are not variables in C Pointers to functions are Things you can do with a function pointer Assignment Store an array of function pointers Use as a function argument Return function pointer from a function Ex. int (*foo)(char *, char *); foo is a pointer to a function that takes two char * arguments and returns an int; Parenthesis needed int * foo(char *, char *); foo is a function that takes two char * arguments and returns an int *.

Function Pointers Why parenthesis around function pointer name: precedence of * vs () Typedef function pointer typedef int (*FOO)(int, int); FOO now has the type pointer to function that takes two int arguments Store a function in a function pointer int bar(int a, int b){ return a + b; } int (*foo)(int, int); foo = &bar; FOO f = &bar; https://stackoverflow.com/questions/4295432/typedef-function-pointer

Unions Unions are variables that use the same memory area to hold objects of different types and possibly sizes Only one object can be stored at any one time Bits in memory do not change only how they are interpreted Programmers job to keep track of what type is currently being stored in the union Same operations as a struct: . for union variable member -> for union pointer member Assign to Copy Take address of

Unions Ex. Can be members of structs or have structs as members int main() { union tag { float f; int x; char *s; } t; // all members reference the same memory/data/bits t.f = 42.79; printf("%f\n", t.f); // treat bits in t as a float printf("%d\n", t.x); // treat bits in t as an int } Can be members of structs or have structs as members Can only be initialized with a value of the same type as the first member In this case float

Bit Fields Bit Fields are a way to directly access bits Ex. Save space Change individual bit values without masks Ex. x = x | 0xFFFF; Implementation dependent Not very portable Fields declared as ints Specify unsigned or signed for better portability Fields behave like small integers Ex. Struct car{ unsigned int ignition_on : 1; unsigned int : 7 // unnamed fields used as padding unsigned int engine_status : 3; // fields can have different width } p_car; p_car.ignition_on = 1; // easy to change a bits value