CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

Similar documents
Procedural programming with C

Lab # 4. Files & Queues in C

Pointers and Structure. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

Variation of Pointers

CS240: Programming in C

Language comparison. C has pointers. Java has references. C++ has pointers and references

CS349/SE382 A1 C Programming Tutorial

CS113: Lecture 5. Topics: Pointers. Pointers and Activation Records

Character Strings. String-copy Example

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Review of the C Programming Language for Principles of Operating Systems

CSCI 171 Chapter Outlines

CS240: Programming in C

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Review of the C Programming Language

EM108 Software Development for Engineers

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Pointers and Arrays 1

CS 61C: Great Ideas in Computer Architecture Introduction to C

More about BOOLEAN issues

MYcsvtu Notes LECTURE 34. POINTERS

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

ELEC 377 C Programming Tutorial. ELEC Operating Systems

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Functions. Arash Rafiey. September 26, 2017

Lecture 6. Statements

Lab 3. Pointers Programming Lab (Using C) XU Silei

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Compiling and Running a C Program in Unix

Functions in C C Programming and Software Tools

C Programming Review CSC 4320/6320

Structs and Alignment CSE 351 Spring

Advanced Pointer & Data Storage

BSM540 Basics of C Language

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

EL2310 Scientific Programming

C Pointers. Indirection Indirection = referencing a value through a pointer. Creating Pointers. Pointer Declarations. Pointer Declarations

Creating a C++ Program

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

Computer System and programming in C

Programming. Structures, enums and unions

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Data Types. Data Types. Integer Types. Signed Integers

CS13002 Programming and Data Structures, Spring 2005

Approximately a Final Exam CPSC 206

CSE 303 Lecture 8. Intro to C programming

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

CS 61c: Great Ideas in Computer Architecture

Final CSE 131B Spring 2004

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

Unit IV & V Previous Papers 1 mark Answers

Short Notes of CS201

Fundamental of Programming (C)

Class Information ANNOUCEMENTS

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

Storage class and Scope:

Bit Manipulation in C

3/22/2016. Pointer Basics. What is a pointer? C Language III. CMSC 313 Sections 01, 02. pointer = memory address + type

JTSK Programming in C II C-Lab II. Lecture 3 & 4

The output will be: marks all or nothing. 1 #include <stdio.h> 2 main() { 3 int i; int j; 4 int *p; int *q; 6 p = &i; 7 q = &j; 8 i = 1;

CS201 - Introduction to Programming Glossary By

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

More Pointers Week 11

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Arrays and Pointers. CSE 2031 Fall November 11, 2013

A Fast Review of C Essentials Part I

Midterm CSE 131 Winter 2014

Principles of C and Memory Management

by Pearson Education, Inc. All Rights Reserved.

Fundamentals of Programming

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

ET156 Introduction to C Programming

ECE 15B COMPUTER ORGANIZATION

Fundamentals of Programming Session 14

Arrays, Pointers and Memory Management

Kurt Schmidt. October 30, 2018

Reserved Words and Identifiers

MODULE 5: Pointers, Preprocessor Directives and Data Structures

WARM UP LESSONS BARE BASICS

Structured programming

Arrays and Pointers (part 1)

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Informatica e Sistemi in Tempo Reale

1.1 Introduction to C Language. Department of CSE

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

CSE 374 Programming Concepts & Tools

IV Unit Second Part STRUCTURES

Tin học cơ sở 4$ Structures!

Transcription:

Summer 2013 CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization Kitty Reeves MTWR 10:20am-12:25pm 1

Structures What is a structure? One or more values, called members, with possibly dissimilar types that are stored together. Used to group together different types of variables under the same name. Aggregates a fixed set of labeled objects, possibly of different types, into a single object (like a record) What is a structure NOT? Since members are NOT the same type/size, they are not as easy to access as array elements that are the same size. Structure variable names are NOT replaced with a pointer in an expression (like arrays) A structure is NOT an array of its members so can NOT use subscripts. 2

Structure Declarations (preview) struct tag {member_list} variable_list; struct S { float b; } x; Declares x to be a structure having two members, a and b. In addition, the structure tag S is created for use in future declarations. struct { float b; } z; Omitting the tag field; cannot create any more variables with the same type as z struct S { float b; }; Omitting the variable list defines the tag S for use in later declarations struct S y; Omitting the member list declares another structure variable y with the same type as x struct S; Incomplete declaration which informs the compiler that S is a structure tag to be defined later 3

Struct storage issues A struct declaration consists of a list of fields, each of which can have any type. The total storage required for a struct object is the sum of the storage requirements of all the fields, plus any internal padding. RULE: the address/offset of a declared memory location *must be* a multiple of the size (in bytes) of its declared type. the reason for this rule is because of addressability which are rules for the alignment of values in memory A struct has no place in memory until a variable has been assigned to it. 4

Structure Declarations This declaration introduces the type struct fraction (both words are required) as a new type. C uses the period (.) to access the fields in a record. You can copy two records of the same type using a single assignment statement, however == does not work on structs https://www.classle.net/faq/why-can t-structures-be-compared-c struct fraction { int numerator; int denominator; }; struct fraction f1, f2; f1.numerator = 25; f1.denominator = 10; f2 = f1; // can t initialize // declare two fractions // this copies over the whole struct 5

Structure Declarations There is a new type called telephone Before you can use the new type, you have to create a variable of that type To access the members of the structure, you must use a dot between the structure name and the variable name #include<stdio.h> struct telephone { char *name; int number; }; int main() { struct telephone index; index.name = "Jane Doe"; index.number = 12345; printf("name: %s\n", index.name); printf("telephone number: %d\n",index.number); return 0; } 6

Structure Declarations So tag, member_list and variable_list are all optional, but cannot all be omitted; at least two must appear for a complete declaration. struct { char b; float c; } x; struct { char b; float c; } y[20], *z; Single variable x contains 3 members Treated different by the compiler DIFFERENT TYPES i.e. z = &x is ILLEGAL So all structures of a given type must be created in a single declaration? NO. An array of 20 structures (y); and A pointer to a structure of this type (z) 7

More Structure Declarations The TAG field Allows a name to be given to the member list so that it can be referenced in subsequent declarations Allows many declarations to use the same member list and thus create structures of the same type struct SIMPLE { char b; float c; } ; Associates tag with member list; does not create any variables So struct SIMPLE x; struct SIMPLE y[20], *z; Now x, y, and z are all the same kind of structure 8

In-class Assignment How are structure members different from array elements? Consider the type, the name and any memory accessing issues. Complete the following declaration to initialize x so that the member a is three, b is the string hello and c is zero: struct { char b[10]; float c; } x; Given: struct abc { int b; int c;}; How do you access member a? 9

More on Structure Declarations MEMBERS Any kind of variable that can be declared outside a structure may also be used as a structure member. Structure members can be scalars, arrays, pointers and even other structures. ACCESS using dot operator Two operands Left = name of structure variable Right = name of the desired member Result = the designated member OPERATOR PRECEDENCE The subscript and dot operators have the same precedence and all associate left to right. The dot operator has higher precedence then the indirection Pointer2Structure operator Left = *must* be a pointer to a structure Right = member Example (*sp).a == sp a Indirection built into arrow/infix operator Follow the address to the structure struct COMPLEX { float f; int a[20]; long *lp; struct SIMPLE s; struct SIMPLE sa[10]; struct SIMPLE *sp; } cmplx, cmp[10]; 10

Structure example struct SIMPLE { char b; float c; } ; struct COMPLEX { float f; int a[20]; long *lp; struct SIMPLE s; struct SIMPLE sa[10]; struct SIMPLE *sp; } cmplx, cmp[10]; cmplx.a[1] = 1; cmplx.s.a = 2; cmplx.sa[1].b = 'A'; cmplx.sp = &cmplx.s; cmp[1].f = 3.14; cmp[5].s.a = 3; cmp[7].sa[2].b = 'B'; int z = cmplx.a[1]; int j = cmplx.s.a; char k = cmplx.sa[1].b; int x = cmplx.sp->a; // (*cmplx.sp).a float r = cmp[1].f; int t = cmp[5].s.a; char y = cmp[7].sa[2].b; 11

Read/Write example % more lab3ch.c #include <stdio.h> main() { struct incheck { int input1; char input2[10]; float input3; } sinput; char again; int x; for (x = 1; x <= 5; x++) { printf("\nenter data\n"); scanf("%d",&sinput.input1); printf("input1 is %d\n",sinput.input1); scanf("%s",sinput.input2); printf("input2 is %s\n",sinput.input2); scanf("%f",&sinput.input3); printf("input3 is %f\n",sinput.input3); // notice the & on 2 of the 3 scanf stmts } } 12

Typedefs typedef <type> <name>; Ex1: #define true 1 #define false 0 Define a boolean type typedef int bool; bool flag = false; Ex2: char *ptr_to_char; typedef char* ptr_to_char; ptr_to_char a; #define token [value] Notice that the name for a #define is listed first, but the name for the typedef is listed second // new variable // new type // new variable 13

Typedefs (vs #define) Define two new types, real and letter. These new types can then be used in the same way as the predefined C types: Types defined: typedef float real; typedef char letter; Variables declared: real sum=0.0; letter nextletter; Typedef vs #define typedef is a compiler token* the preprocessor does not care about it obeys scoping rules just like variables #define is a preprocessor token the compiler itself will never see it stays valid until the end of the compilation unit #define MY_TYPE int find and replace* typedef int My_Type; updated C code typedef int* int_p1; int_p1 a, b, c; // a, b, and c are all int pointers // int a, b, c; all are int s #define int_p2 int* int_p2 a, b, c; // int* a,b,c; // only the first is a pointer! 14

Using typedefs with Structures A typedef statement introduces a shorthand name for a type. shorter to write can simplify more complex type definitions struct { char b; float c; } x; typedef struct { char b; float c; } Simple; see slide#8 So Simple x; Simple y[20], *z; Now x, y, and z are all the same TYPE. Similar to these being all integers i.e. all the same TYPE int x; int y[20], *z; 15

Typedef Structure Example #include <stdio.h> typedef struct { int x; int y; } point; int main(void) { /* Define a variable p of type point, and initialize all its members inline! */ point p = {1,2}; // p.x = 1 and p.y = 2 point q; // can put on above line q = p; // q.x = 1 and q.y=2 q.x = 2; /* Demonstrate we have a copy and that they are now different. */ if (p.x!= q.x) printf("the members are not equal! %d!= %d", p.x, q.x); return 0; } Another example: see struct4.c 16

Structures and Pointers #include<stdio.h> typedef struct { char *name; int number; } TELEPHONE; What is going on here? Remember: TELEPHONE is a type of structure; -> is a struct member through pointer operator (called infix or member access from a pointer) see operator precedence (top) int main() { TELEPHONE index; TELEPHONE *ptr_myindex; ptr_myindex = &index; ptr_myindex->name = "Jane Doe"; // (*ptr_myindex).name ptr_myindex->number = 12345; // (*ptr_myindex).number printf("name: %s\n", ptr_myindex->name); printf("telephone number: %d\n", ptr_myindex->number); return 0; } 17

Structures and Pointers #include<stdio.h> #include <stdlib.h> typedef struct rec { int i; float PI; char A; } RECORD; int main() { RECORD *ptr_one; ptr_one = (RECORD *) malloc (sizeof(record)); (*ptr_one).i = 10; (*ptr_one).pi = 3.14; (*ptr_one).a = 'a'; printf("first value: %d\n",(*ptr_one).i); printf("second value: %f\n", (*ptr_one).pi); printf("third value: %c\n", (*ptr_one).a); free(ptr_one); return 0; } rec is not necessary for given/left code, but *is* necessary for below code update For below, without RECORD, warning: useless storage class specifier in empty declaration struct rec *ptr_one; ptr_one =(struct rec *) malloc (sizeof(struct rec)); ptr_one->i = 10; ptr_one->pi = 3.14; ptr_one->a = 'a'; printf("first value: %d\n", ptr_one->i); printf("second value: %f\n", ptr_one->pi); printf("third value: %c\n", ptr_one->a); 18

Structures and Pointers how set "pb" to be a pointer to member b within structure mystruct? offsetof tells you the offset of a variable within a structure (stddef.h) struct mystruct { char* b; } ; //note: could put st here instead struct mystruct st; char* pb = (char*)&st + offsetof(struct mystruct, b); Char* has higher precedence than + but this still works right 19

File I/O or initialization lab3chfileio.c 1 abc 2 3 def 4 5 ghi 6 7 jkl 8 9 mno 10 lab3chin 1 abc 2 3 def 4 5 ghi 6 7 jkl 8 9 mno 10 lab3chin2 INITIALIZE but what if want to read from a file? struct simple { int input1; char input2[10]; float input3; } input[recs]; // initialize structure instead of read in from a file struct simple input[recs] = { {1,{'a','b','c'},2}, {3, {'d','e','f'}, 4}, {5, {'g','h','i'}, 6}, }; 20

Example continued char again, ch; int x=0; fscanf(fpin,"%d %s %f",&input[x].input1,input[x].input2, &input[x].input3); fprintf(fpout,"input1 is %d\ninput2 is %s\ninput3 is %f\n", input[x].input1,input[x].input2,input[x].input3); x++; fscanf(fpin, "%d",&input[x].input1); while(x < recs-1 &&!feof(fpin)) { fprintf(fpout,"input1 is %d\n",input[x].input1); fscanf(fpin, "%s",input[x].input2); fprintf(fpout,"input2 is %s\n",input[x].input2); fscanf(fpin, "%f",&input[x].input3); fprintf(fpout,"input3 is %f\n",input[x].input3); // notice the & on 2 of the 3 scanf stmts x++; fscanf(fpin, "%d",&input[x].input1); } } 21

Structure memory (again) What does memory look like? typedef struct { short b[2]; } Ex2; typedef struct EX { char b[3]; Ex2 c; struct EX *d; } Ex; px x Given the following declaration, fill in the above memory locations: Ex x = { 10, Hi, { 5, { -1, 25 } }, 0 }; Ex *px = &x; 22

In-class exercise Missing values cause the remaining members to get default initialization whatever that might be! typedef struct { char b; float c; } Simple; struct INIT_EX { short b[10]; Simple c; } x = { 10, { 1, 2, 3, 4, 5 }, { 25, x, 1.9 } } ; What goes here (hint in blue below)? struct INIT_EX y = { 0, {10, 20, 30, 40, 50, 60, 70, 80, 90, 100 }, { 1000, a, 3.14 } } ; Name all the variables and their initial values: y.a = 0; y.b[0] = 10; y.b[1] = 20; y.b[2] = 30; etc y.c.a = 1000; y.c.b = a ; y.c.c = 3.14; 23

Stop here 24

Self-Referential Structures Illegal - infinite struct SELF_REF { struct SELF_REF b; int c; } ; Watch out typedef struct { struct SELF_REF *b; int c; } SELF_REF ; Correction struct SELF_REF { struct SELF_REF *b; int c; } ; Correction typedef struct SELF_REF_TAG { struct SELF_REF_TAG *b; int c; } SELF_REF ; 25

Incomplete Declarations Structures that are mutually dependent As with self referential structures, at least one of the structures must refer to the other only through pointers So, which one gets declared first??? struct B; struct A { struct B *partner; /* etc */ } ; Declares an identifier to be a structure tag Use this tag in declarations where the size of the structure is not needed (pointer!) Needed in the member list of A struct B { struct A *partner; /* etc */ } ; Doesn t have to be a pointer 26

Structures as Function arguments Legal to pass a structure to a function similar to any other variable but often inefficient /* electronic cash register individual transaction receipt */ #define PRODUCT_SIZE 20; typedef struct { char product[product_size]; int qty; float unit_price; float total_amount; } Transaction; Function call: print_receipt(current_trans); Copy by value copies 32 bytes to the stack which can then be discarded later Instead (Transaction *trans) trans->product // fyi: (*trans).product trans->qty trans->unit_price trans->total_amount print_receipt(&current_trans); void print_receipt(transaction *trans) void print_receipt (Transaction trans) { printf( %s\n, trans.product); printf(%d @ %.2f total %.2f\n, trans.qty, trans.unit_price, trans.total_amount); } 27

Function pointers and Typedef We can use function pointers in return values as well // this function called functionfactory receives parameter n // and returns a pointer to another function which receives // two integers and it returns another integer int (*functionfactory(int n))(int, int) { printf("got parameter %d", n); int (*functionptr)(int,int) = &addint; return functionptr; } But it's much nicer to use a typedef: typedef int (*myfuncdef)(int, int); // note that the typedef name is indeed myfuncdef myfuncdef functionfactory(int n) { printf("got parameter %d", n); myfuncdef functionptr = &addint; return functionptr; } 28

Typedef with Function Pointers Original function definition is: float (*GetPtr1(const char opcode))(float, float) Using a typedef, define a pointer to a function which takes two floats and returns a float typedef float(*pt2func)(float, float); Then you can change the function definition to: pt2func GetPtr1(const char opcode) Define an Array of Function Pointers: int (*funcarr2[10])(float, char, char) = {NULL}; Using a typedef: typedef int (*pt2function)(float, char, char); You can define an array of function pointers: pt2function funcarr1[10] = {NULL}; 29