Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
|
|
- Cecilia Hancock
- 5 years ago
- Views:
Transcription
1 Slide Set 6 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017
2 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 2/38 Contents Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards
3 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 3/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards
4 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 4/38 Introduction to structure types in C A variable of a structure type can contain multiple pieces of data, with different types allowed for the different pieces. The keyword struct is used to create a structure type, and sometimes structure types are called struct types. How is a structure type different from an array type?
5 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 5/38 As an example, a structure type could be used for information related to single student. What would be reasonable choices for type of storage in the table below? piece of data last name given names ID number type of storage Let s write some C code for a type that can hold a student s last name, given names, and ID number.
6 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 6/38 Example structure variable declarations The code written in response to the previous slide created a type called struct StudentInfo. So what do each of the following variable declarations do? struct StudentInfo a; // line 1 struct StudentInfo *b; // line 2 struct StudentInfo c[100]; // line 3
7 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 7/38 Accessing members of structure variables The individual variables within a structure variable are called members. (Other words sometimes used for members are fields, or components.) A member can be accessed using the structure member operator, which is a dot. There is example code on the next slide. Let s draw the AR of main at point (1). (But let s simplify the pictures of the arrays of char so we don t have to draw 96 array elements!)
8 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 8/38 #include <string.h> #define directives and declaration of struct StudentInfo int main(void) { struct StudentInfo s; strcpy(s.last_name, "Coyote"); strcpy(s.given_names, "Wile E."); s.id_number = ; // (1) } return 0; Now let s rewrite main to get the same effect, using a thing called a structure initializer.
9 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 9/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards
10 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 10/38 Creating alternate names for types with typedef typedef declarations do not define new types. Instead they define aliases (alternate names) for existing types. The simplest syntax for typedef looks like this: typedef variable declaration (Note that the variable declaration includes a semicolon.) The new name for a type goes in the spot where the name of the variable would appear in a normal variable declaration. So, what do these lines of code mean? typedef int FOO; // line 1 typedef double *PTR2DBL; // line 2 typedef struct StudentInfo stu_info_t; // line 3
11 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 11/38 Creating aliases for structure type names in C is common, because it is annoying to have to key in the word struct many times. This is how I would set up the struct StudentInfo type, and then create an handy alternate name for the type... #define LAST_NAME_MAX_LENGTH 31 #define GIVEN_NAME_MAX_LENGTH 63 struct StudentInfo { char last_name[last_name_max_length + 1]; char given_names[given_name_max_length + 1]; int id_number; }; typedef struct StudentInfo stu_info_t;
12 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 12/38 On the previous slide, there is a clear separation between the creation of the type and and the creation of an alternate name for the type. It s possible to save even more keystrokes by merging the type declaration and the typedef into a single declaration... #define LAST_NAME_MAX_LENGTH 31 #define GIVEN_NAME_MAX_LENGTH 63 typedef struct StudentInfo { char last_name[last_name_max_length + 1]; char given_names[given_name_max_length + 1]; int id_number; } stu_info_t; The code above has exactly the same effect as the code on Slide 11.
13 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 13/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards
14 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 14/38 Operations allowed with structure types Let s define the term structure object to refer to variables and function arguments of structure types. (Later we ll extend the definition to include some other kinds of data related to structure types.) Here are some (not all) of the things you can do with a structure object: access members using the. operator take its address with the & operator copy it, one of three ways: assignment with =, passing a function argument, passing a function return value
15 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 15/38 And here are some things you can t do with structure objects: arithmetic with operators such as + - * / % ++ --, etc. comparison with any of ==!= < <= > >= In C++ you can use operator overloading to define meanings for the above operators when used with structure types. You can t do that in C.
16 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 16/38 To make a simple example of copying structure objects, let s consider the example of representing vectors in the (x,y) plane, and writing a C function to compute the effect of rotating a vector by 90 degrees. y 90 degrees x What code do we need to declare a type called struct vec2d and an alias for that type called vec2d_t?
17 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 17/38 The formula for 90-degree (counterclockwise) rotation is simple. If vector w is the 90-degree rotation of vector v, then x component of w = (y component of v) y component of w = x component of v Let s write a function definition to correspond to this prototype: vec2d_t rotate90(vec2d_t vec); Then let s write a simple main function to call rotate90 and draw diagrams to show how arguments and return values get copied.
18 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 18/38 Attention: Rules for structures are not the same as the rule for arrays! Arrays are potentially large objects that could be expensive to copy. Structure objects are also potentially large things that could be expensive to copy. Using an array name as a function argument results in copying only the address of element 0, a relatively small thing. Therefore, you might guess that using a structure object name as a function argument also just sends an address to the called function. That guess would be wrong. We ve just seen that using a structure object as a function argument results in copying the whole object.
19 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 19/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards
20 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 20/38 Pointers to structure objects These are used a lot in real-world C code. As with any other data type, the address of a struct object is the lowest address of all of the bytes allocated for the object. A pointer-to-structure contains a a single address only, the address of an entire structure object.
21 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 21/38 Experience has shown me that it s very tempting to conclude that a pointer to a structure object that has, say, three members, must contain three different addresses. My answer to that is: No, no, no, really, No! Let s draw a diagram for an example: struct Foo { double d; int i; char c[4]; }; int main(void) { struct Foo f = { 2.25, 999, "ABC" }; struct Foo *p = &f; // (1) return 0; }
22 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 22/38 The -> operator This is an arrow made from minus and greater-than, with no space in between. If p is a pointer to a structure object, then p-> member name has exactly the same meaning as (*p). member name The parentheses above are important dot has higher precedence than star, so *p. member name is equivalent to *(p. member name ), which makes no sense if p is some kind of pointer.
23 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 23/38 Let s look at an alternate way to implement the rotate90 function, with this prototype: void rotate90alt(vec2d_t *out, const vec2d_t *in); in points to the original vector, and out points to a vector that will get the result of rotating the original vector by 90 degrees. What should the function definition of rotate90alt be? Let s write a simple main function to call rotate90alt and draw diagrams to show how arguments are used.
24 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 24/38 Pointers to data within structure objects It s permissible and often useful to have pointers point to members within structure objects, but these pointers must have appropriate types. Here is an example, which continues on the next slide... #include <string.h> struct table_entry { int number; char symbol[3]; double weight; }; typedef struct table_entry table_entry_t;
25 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 25/38 void titanium(table_entry_t *te); int main(void) { table_entry_t x; titanium(&x); return 0; } void titanium(table_entry_t *te) { te->number = 22; strcpy(te->symbol, "Ti"); te->weight = 47.90; } Let s make a diagram for the moment when the strcpy function starts work.
26 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 26/38 Why use pointers to structures as function arguments? Answer: To save time and memory space. Remember that a pointer to a structure object is just a single address, the lowest of all the addresses of all the bytes used for the structure object. So a pointer typically occupies much less space in an AR than a structure object. And copying an address, which happens when a pointer is sent as a function argument, takes much less time than copying a large structure object.
27 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 27/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards
28 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 28/38 An earlier version of this example did not include the declaration and initialization of q. struct Foo { double d; int i; char c[4]; }; int main(void) { struct Foo f = { 2.25, 999, "ABC" }; struct Foo *p = &f; double *q = &f.d; // (1) return 0; } If we were told what address is in p at point (1), what could we say about the address in q at that same moment? So what is the difference between p and q?
29 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 29/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards
30 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 30/38 Structures within structures It is common to declare a structure type in which one or more members have other structure types. Suppose in some program we would like to keep track of circles in an (x,y)-plane each circle has a center and a radius. y radius center x
31 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 31/38 Suppose that this code from earlier in this Slide Set is available... struct vec2d { double x; double y; }; typedef struct vec2d vec2d_t; How could we set up a structure type for circles, using a member of type vec2d_t to specify the center of a circle? Let s write a simple main function to demonstrate use of some variables of our new structure type.
32 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 32/38 Outline of Slide Set 6 Introduction to structure types in C Creating alternate names for types with typedef Operations allowed with structure types Pointers to structure objects A little extension to a previous example Structures within structures Include guards
33 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 33/38 Include guards Here s a reminder of an important definition: A translation unit is the output the C preprocessor produces starting from a single.c file. So a translation unit may contain contents from one or more files included using #include, macro expansions based on #define directives, and so on. It s an error to repeat certain kinds of C code in a translation unit. Among these kinds of code are structure type declarations typedef declarations various other things
34 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 34/38 An include guard is a preprocessor trick used to avoid inadvertent repetition of code within a translation unit. Before we look at how to write an include guard, let s look at how things can go wrong without include guards. Suppose Coder A writes these two tiny header files: file vec2d.h struct vec2d { double x; double y; }; typedef struct vec2d vec2d_t; file circle.h #include "vec2d.h" struct circle { vec2d_t center; double radius; }; typedef struct circle circle_t; Then Coder A gives the header files to Coder B...
35 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 35/38 Coder B wants to write a program with some variables of type vec2d_t and some other variables of type circle_t, and writes something like this... file myprog.c #include <stdio.h> #include "vec2d.h" #include "circle.h" int main(void) { vec2d_t v; circle_t c; //... } Why will a C compiler reject Coder B s program? What would be a very simple fix for this problem?
36 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 36/38 It turns out that the simple fix offered in response to a question on the last slide does not scale it s not a reasonable solution for a project with many large header files, many of which #include some of the other header files. A solution that scales well is shown on the next slide. In each of the two header files, the first, second and last lines form what are known as include guards. Let s briefly explain why the include guards make the following sequence of directives safe and effective... #include "vec2d.h" #include "circle.h"
37 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 37/38 file vec2d.h #ifndef VEC2D_H #define VEC2D_H struct vec2d { double x; double y; }; typedef struct vec2d vec2d_t; #endif file circle.h #ifndef CIRCLE_H #define CIRCLE_H #include "vec2d.h" struct circle { vec2d_t center; double radius; }; typedef struct circle circle_t; #endif Attention: It s customary to make the name of the macro used in an include guard similar to the name of the header file it appears in for example, VEC2D_H is obviously derived from vec2d.h. That helps with readability, but is not mandatory the preprocessor does not care whether the macro name is similar to the file name.
38 ENCM 339 Fall 2017 Section 01 Slide Set 6 slide 38/38 Final notes about include guards These are really common in practical C projects! For example, on a Linux system, the first non-comment two lines of <string.h> are #ifndef _STRING_H #define _STRING_H 1 and the last line is #endif /* string.h */
Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 5 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2016 ENCM 339 Fall 2016 Slide Set 5 slide 2/32
More informationSlide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 3 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01
More informationSlide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 3 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 3 slide 2/46
More informationSlide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 1 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016 ENCM 339 Fall 2016 Slide Set 1 slide 2/43
More informationSlide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng
Slide Set 9 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2018 ENCM 335 Fall 2018 Slide Set 9 slide 2/32
More informationSlide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng
Slide Set 2 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 2 slide
More informationSlide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng
Slide Set 4 for ENCM 335 in Fall 2018 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2018 ENCM 335 Fall 2018 Slide Set 4 slide
More informationENCM 339 Fall 2017 Tutorial for Week 8
ENCM 339 Fall 2017 Tutorial for Week 8 for section T01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 2 November, 2017 ENCM 339 T01 Tutorial
More informationSlide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 14 for ENCM 339 Fall 2015 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2015 SN s ENCM 339 Fall 2015 Slide Set 14 slide
More informationSlide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 4 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2017 ENCM 339 Fall 2017 Section 01
More informationSlide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 8 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2017 ENCM 339 Fall 2017 Section 01 Slide
More informationSlide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 14 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2016 ENCM 339 Fall 2016 Slide Set 14 slide 2/35
More informationSlide Set 5. for ENCM 369 Winter 2014 Lecture Section 01. Steve Norman, PhD, PEng
Slide Set 5 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section
More informationKurt Schmidt. October 30, 2018
to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed
More informationContents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides
Slide Set 1 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section
More informationQUIZ. What are 3 differences between C and C++ const variables?
QUIZ What are 3 differences between C and C++ const variables? Solution QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Solution The C/C++ preprocessor substitutes mechanically,
More informationSlide Set 1 (corrected)
Slide Set 1 (corrected) for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018
More informationCOSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26
COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.
More informationContents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs
Slide Set 2 for ENCM 369 Winter 2014 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2014 ENCM 369 W14 Section
More informationMotivation was to facilitate development of systems software, especially OS development.
A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.
More informationSlide Set 18. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 18 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary December 2016 ENCM 339 Fall 2016 Slide Set 18 slide 2/26
More informationSlide Set 18. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
Slide Set 18 for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary December 2017 ENCM 339 Fall 2017 Section 01
More informationUniversity of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman
page 1 of 6 University of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman Fall 2017 MIDTERM TEST Wednesday, November 1 7:00pm to 9:00pm This
More informationProgramming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur
Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 08 Constants and Inline Functions Welcome to module 6 of Programming
More informationThe University of Calgary. ENCM 339 Programming Fundamentals Fall 2016
The University of Calgary ENCM 339 Programming Fundamentals Fall 2016 Instructors: S. Norman, and M. Moussavi Wednesday, November 2 7:00 to 9:00 PM The First Letter of your Last Name:! Please Print your
More informationSlide Set 5. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng
Slide Set 5 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary February 2018 ENCM 369 Winter 2018 Section
More informationSlide Set 4. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng
Slide Set 4 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018 Section
More informationSlide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng
Slide Set 3 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary January 2018 ENCM 369 Winter 2018 Section
More informationMotivation was to facilitate development of systems software, especially OS development.
A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.
More informationCS 61c: Great Ideas in Computer Architecture
Arrays, Strings, and Some More Pointers June 24, 2014 Review of Last Lecture C Basics Variables, functioss, control flow, types, structs Only 0 and NULL evaluate to false Pointers hold addresses Address
More informationCSE 333 Midterm Exam 2/14/14
Name There are 4 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed
More informationOperator overloading
1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The
More informationEL6483: Brief Overview of C Programming Language
EL6483: Brief Overview of C Programming Language EL6483 Spring 2016 EL6483 EL6483: Brief Overview of C Programming Language Spring 2016 1 / 30 Preprocessor macros, Syntax for comments Macro definitions
More informationC BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.
C BOOTCAMP DAY 2 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh Pointers 2 Pointers Pointers are an address in memory Includes variable addresses,
More informationFundamentals of Programming Session 4
Fundamentals of Programming Session 4 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2011 These slides are created using Deitel s slides, ( 1992-2010 by Pearson Education, Inc).
More informationCSE 333 Midterm Exam 5/10/13
Name There are 5 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed
More informationUniversity of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman
page 1 of 6 University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman Fall 2018 MIDTERM TEST Thursday, November 1 6:30pm to 8:30pm Please do not write your
More informationENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2
page 1 of 11 ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2 Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2017 Lab instructions and
More informationENCM 501 Winter 2019 Assignment 9
page 1 of 6 ENCM 501 Winter 2019 Assignment 9 Steve Norman Department of Electrical & Computer Engineering University of Calgary April 2019 Assignment instructions and other documents for ENCM 501 can
More informationCS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010
CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011 Lectures 1-22 Moaaz Siddiq Asad Ali Latest Mcqs MIDTERM EXAMINATION Spring 2010 Question No: 1 ( Marks: 1 ) - Please
More informationQUIZ. Source:
QUIZ Source: http://stackoverflow.com/questions/17349387/scope-of-macros-in-c Ch. 4: Data Abstraction The only way to get massive increases in productivity is to leverage off other people s code. That
More informationCS102: Variables and Expressions
CS102: Variables and Expressions The topic of variables is one of the most important in C or any other high-level programming language. We will start with a simple example: int x; printf("the value of
More informationC++ for Java Programmers
Basics all Finished! Everything we have covered so far: Lecture 5 Operators Variables Arrays Null Terminated Strings Structs Functions 1 2 45 mins of pure fun Introduction Today: Pointers Pointers Even
More informationGoals of this Lecture
C Pointers Goals of this Lecture Help you learn about: Pointers and application Pointer variables Operators & relation to arrays 2 Pointer Variables The first step in understanding pointers is visualizing
More informationIntroduction to C: Pointers
Introduction to C: Pointers Nils Moschüring PhD Student (LMU) Nils Moschüring PhD Student (LMU), Introduction to C: Pointers 1 1 Introduction 2 Pointers Basics Useful: Function
More informationCSE 333 Midterm Exam Sample Solution 5/10/13
Question 1. (18 points) Consider these two C files: a.c void f(int p); int main() { f(17); return 0; b.c void f(char *p) { *p = 'x'; (a) Why is the program made from a.c and b.c incorrect? What would you
More informationSlide Set 9. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng
Slide Set 9 for ENCM 369 Winter 2018 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary March 2018 ENCM 369 Winter 2018 Section 01
More informationCSCI 171 Chapter Outlines
Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures
More informationCSE 333 Midterm Exam 5/9/14 Sample Solution
Question 1. (20 points) C programming. Implement the C library function strncpy. The specification of srncpy is as follows: Copy characters (bytes) from src to dst until either a '\0' character is found
More informationENCM 335 Fall 2018 Tutorial for Week 13
ENCM 335 Fall 2018 Tutorial for Week 13 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 06 December, 2018 ENCM 335 Tutorial 06 Dec 2018 slide
More informationPointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory
Pointers A pointer is simply a reference to a variable/object Compilers automatically generate code to store/retrieve variables from memory It is automatically generating internal pointers We don t have
More informationCSE P 501 Exam 12/1/11
Name There are 7 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. You may refer to the following references:
More informationChapter 1 Getting Started
Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different
More information211: Computer Architecture Summer 2016
211: Computer Architecture Summer 2016 Liu Liu Topic: C Programming Structure: - header files - global / local variables - main() - macro Basic Units: - basic data types - arithmetic / logical / bit operators
More informationCS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco
CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level
More informationSlide Set 5. for ENEL 353 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
Slide Set 5 for ENEL 353 Fall 207 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 207 SN s ENEL 353 Fall 207 Slide Set 5 slide
More informationCIS 190: C/C++ Programming. Classes in C++
CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:
More informationTopic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History
Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program
More informationCSE 333 Midterm Exam 7/29/13
Name There are 5 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed
More informationP.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above
P.G.TRB - COMPUTER SCIENCE Total Marks : 50 Time : 30 Minutes 1. C was primarily developed as a a)systems programming language b) general purpose language c) data processing language d) none of the above
More informationADTs & Classes. An introduction
ADTs & Classes An introduction Quick review of OOP Object: combination of: data structures (describe object attributes) functions (describe object behaviors) Class: C++ mechanism used to represent an object
More informationIntroduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files
Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #54 Organizing Code in multiple files (Refer Slide Time: 00:09) In this lecture, let us look at one particular
More informationHere's how you declare a function that returns a pointer to a character:
23 of 40 3/28/2013 10:35 PM Violets are blue Roses are red C has been around, But it is new to you! ANALYSIS: Lines 32 and 33 in main() prompt the user for the desired sort order. The value entered is
More informationCSE 333 Midterm Exam 7/22/12
Name There are 6 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes, closed
More informationENCM 339 Fall 2017: Editing and Running Programs in the Lab
page 1 of 8 ENCM 339 Fall 2017: Editing and Running Programs in the Lab Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2017 Introduction This document is a
More informationName: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015
CMSC 202 Section 06 Fall 2015 Computer Science II Midterm Exam I Name: Username: Score Max Section: (check one) 07 - Sushant Athley, Tuesday 11:30am 08 - Aishwarya Bhide, Thursday 11:30am 09 - Phanindra
More informationHave examined process Creating program Have developed program Written in C Source code
Preprocessing, Compiling, Assembling, and Linking Introduction In this lesson will examine Architecture of C program Introduce C preprocessor and preprocessor directives How to use preprocessor s directives
More informationTDDE18 & 726G77. Functions
TDDE18 & 726G77 Functions Labs update No more one time password. We will note who have demonstrated during the lab and register this in webreg. Use the terminal to send in your lab! Dont use Visual studio
More informationObject Oriented Design
Object Oriented Design Chapter 9 Initializing a non-static data member in the class definition is a syntax error 1 9.2 Time Class Case Study In Fig. 9.1, the class definition is enclosed in the following
More informationCompiling and Running a C Program in Unix
CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 95 ] Compiling and Running a C Program in Unix Simple scenario in which your program is in a single file: Suppose you want to name
More informationTopic 6: A Quick Intro To C
Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions
More informationENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions
page 1 of 5 ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions Steve Norman Department of Electrical & Computer Engineering University of Calgary October 2018 Lab instructions and
More informationChapter 2, Part I Introduction to C Programming
Chapter 2, Part I Introduction to C Programming C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1 2016 Pearson Education, Ltd. All rights reserved. 2 2016 Pearson Education,
More informationM4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE
M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be
More informationCMPT 115. C tutorial for students who took 111 in Java. University of Saskatchewan. Mark G. Eramian, Ian McQuillan CMPT 115 1/32
CMPT 115 C tutorial for students who took 111 in Java Mark G. Eramian Ian McQuillan University of Saskatchewan Mark G. Eramian, Ian McQuillan CMPT 115 1/32 Part I Starting out Mark G. Eramian, Ian McQuillan
More informationExercise Session 2 Simon Gerber
Exercise Session 2 Simon Gerber CASP 2014 Exercise 2: Binary search tree Implement and test a binary search tree in C: Implement key insert() and lookup() functions Implement as C module: bst.c, bst.h
More informationConverting a Lowercase Letter Character to Uppercase (Or Vice Versa)
Looping Forward Through the Characters of a C String A lot of C string algorithms require looping forward through all of the characters of the string. We can use a for loop to do that. The first character
More informationSlides for Lecture 15
Slides for Lecture 5 ENEL 353: Digital Circuits Fall 203 Term Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October, 203 ENEL 353 F3 Section
More informationPIC 10A Objects/Classes
PIC 10A Objects/Classes Ernest Ryu UCLA Mathematics Last edited: November 13, 2017 User-defined types In C++, we can define our own custom types. Object is synonymous to variable, and class is synonymous
More informationAssignment #5 Answers
Assignment #5 Answers Introductory C Programming UW Experimental College Assignment #5 ANSWERS Question 1. What's wrong with #define N 10;? The semicolon at the end of the line will become part of N's
More informationLecture 12 Modular Programming with Functions
Lecture 12 Modular Programming with Functions Learning Objectives: Understand the purpose of functions Understand how to use functions and the vocabulary Write your own functions 1 Modularity (the purpose
More informationDECLARAING AND INITIALIZING POINTERS
DECLARAING AND INITIALIZING POINTERS Passing arguments Call by Address Introduction to Pointers Within the computer s memory, every stored data item occupies one or more contiguous memory cells (i.e.,
More informationG52CPP C++ Programming Lecture 6. Dr Jason Atkin
G52CPP C++ Programming Lecture 6 Dr Jason Atkin 1 Last lecture The Stack Lifetime of local variables Global variables Static local variables const (briefly) 2 Visibility is different from lifetime Just
More informationCSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community
CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine
More informationLecture 10: building large projects, beginning C++, C++ and structs
CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 10:
More informationCIS 2107 Computer Systems and Low-Level Programming Fall 2010 Midterm
Fall 2010 Name: Page Points Score 1 8 2 9 3 11 4 10 5 11 6 1 7 9 8 21 9 10 10 10 Total: 100 Instructions The exam is closed book, closed notes. You may not use a calculator, cell phone, etc. For each of
More informationData Representation and Storage
Data Representation and Storage Learning Objectives Define the following terms (with respect to C): Object Declaration Definition Alias Fundamental type Derived type Use size_t, ssize_t appropriately Use
More informationENCM 339 Fall 2017 Lecture Section 01 Lab 5 for the Week of October 16
page 1 of 5 ENCM 339 Fall 2017 Lecture Section 01 Lab 5 for the Week of October 16 Steve Norman Department of Electrical & Computer Engineering University of Calgary October 2017 Lab instructions and other
More informationLectures 5-6: Introduction to C
Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most
More informationFundamental of Programming (C)
Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering
More informationAnnotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments
Variables, Data Types, and More Introduction In this lesson will introduce and study C annotation and comments C variables Identifiers C data types First thoughts on good coding style Declarations vs.
More informationCh. 10: Name Control
Ch. 10: Name Control Static elements from C The static keyword was overloaded in C before people knew what the term overload meant, and C++ has added yet another meaning. The underlying concept with all
More informationCOSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22
COSC 2P91 Bringing it all together... Week 4b Brock University Brock University (Week 4b) Bringing it all together... 1 / 22 A note on practicality and program design... Writing a single, monolithic source
More informationCh. 3: The C in C++ - Continued -
Ch. 3: The C in C++ - Continued - QUIZ What are the 3 ways a reference can be passed to a C++ function? QUIZ True or false: References behave like constant pointers with automatic dereferencing. QUIZ What
More informationCSE 333 Midterm Exam July 24, Name UW ID#
Name UW ID# There are 6 questions worth a total of 100 points. Please budget your time so you get to all of the questions. Keep your answers brief and to the point. The exam is closed book, closed notes,
More information#1 #2 with corrections Monday, March 12 7:00pm to 8:30pm. Please do not write your U of C ID number on this cover page.
page 1 of 6 University of Calgary Department of Electrical and Computer Engineering ENCM 369: Computer Organization Lecture Instructors: Steve Norman and Norm Bartley Winter 2018 MIDTERM TEST #1 #2 with
More informationProgramming. Structures, enums and unions
Programming Structures, enums and unions Summary } Structures } Declaration } Member access } Function arguments } Memory layout } Array of structures } Typedef } Enums } Unions 2 Idea! } I want to describe
More informationCS1102: What is a Programming Language?
CS1102: What is a Programming Language? Kathi Fisler, WPI September 13, 2007 1 The Design and Programming Perspectives To start to understand what comprises a programming language, let s consider sample
More informationENCM 335 Fall 2018 Lab 2 for the Week of September 24
page 1 of 8 ENCM 335 Fall 2018 Lab 2 for the Week of September 24 Steve Norman Department of Electrical & Computer Engineering University of Calgary September 2018 Lab instructions and other documents
More informationQUIZ. What is wrong with this code that uses default arguments?
QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code
More information