Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary
|
|
- Branden Bradley
- 5 years ago
- Views:
Transcription
1 Slide Set 3 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary September 2016
2 ENCM 339 Fall 2016 Slide Set 3 slide 2/46 Contents More about pointers as function parameters Arrays in C Brief notes about the Standard C Library Character strings in C The strcpy function and related C concepts Function parameters declared to be arrays are really pointers! Buffer overflows
3 ENCM 339 Fall 2016 Slide Set 3 slide 3/46 Outline of Slide Set 3 More about pointers as function parameters Arrays in C Brief notes about the Standard C Library Character strings in C The strcpy function and related C concepts Function parameters declared to be arrays are really pointers! Buffer overflows
4 ENCM 339 Fall 2016 Slide Set 3 slide 4/46 Comment style for tutorial and lecture slides (This is repeated from a tutorial slide.) int foo(int a) { int b; b = a * 2; } // point one return b + 3; int foo(int a) { int b; b = a * 2; // (1) return b + 3; } // (1) marks the moment in time just after the assignment to b has finished and just before the return statement starts.
5 ENCM 339 Fall 2016 Slide Set 3 slide 5/46 More about pointers as function parameters The code on the next slide is a program from Slide Set 2, presented so that we can briefly review it. Let s make some brief notes about how the program works.
6 #include <stdio.h> void foot_and_inch(int inch_only, int *feet, int *extra_inch); int main(void) { int total_in = 75, ft, in; foot_and_inch(total_in, &ft, &in); printf("%d inches is equal to %d feet, %d inches.\n", total_in, ft, in); return 0; } void foot_and_inch(int inch_only, int *feet, int *extra_inch) { // (1) *feet = inch_only / 12; *extra_inch = inch_only % 12; // (2) }
7 ENCM 339 Fall 2016 Slide Set 3 slide 7/46 A common mistake with pointers Let s replace the definition of main with this defective code: int main(void) { int total_in = 75; int *ft; int *in; foot_and_inch(total_in, ft, in); printf("%d inches is equal to %d feet, %d inches.\n", total_in, *ft, *in); return 0; } Why is the code defective? What will happen if we try to compile and run the modified program?
8 ENCM 339 Fall 2016 Slide Set 3 slide 8/46 A quick remark about the scanf function When scanf was introduced in a lecture, I told you that you had to put &, the address-of operator, in front of the names of the variables that are to receive input. At that time, I couldn t give a very precise reason for doing so. But now you know about addresses and pointer expressions. Suppose k is a variable of type int. Why is it that the following code can t possibly work? scanf("%d", k); And what is so important about & in the following code? scanf("%d", &k);
9 ENCM 339 Fall 2016 Slide Set 3 slide 9/46 Outline of Slide Set 3 More about pointers as function parameters Arrays in C Brief notes about the Standard C Library Character strings in C The strcpy function and related C concepts Function parameters declared to be arrays are really pointers! Buffer overflows
10 ENCM 339 Fall 2016 Slide Set 3 slide 10/46 Arrays in C In many programming languages, an array is a collection of data objects called elements, such that all the elements have the same type; an individual element within an array is selected using an integer expression called an index. In C, arrays are very closely connected to pointers. That s not true in most other programming languages!
11 ENCM 339 Fall 2016 Slide Set 3 slide 11/46 Syntax for declaring an array variable in C For an array element type such as int, double, or char, here is how to declare a C array variable: type identifier [ integer expression ] ; The integer expression specifies the number of elements in the array. In earlier versions of C, it had to be a constant, but as of the 1999 C standard, it can involve variables and function parameters. In examples in ENCM 339 lectures, the number of elements will be a simple constant.
12 ENCM 339 Fall 2016 Slide Set 3 slide 12/46 An example array variable This declares x to be an array of 5 ints: int x[5]; Let s make a sketch of x, and make some remarks. (Among the remarks: Indexing starts at zero.)
13 ENCM 339 Fall 2016 Slide Set 3 slide 13/46 When programming in C, don t try to use Processing syntax! This is how to create an array of 5 int elements in Processing: int[ ] a = new int[5]; That won t work at all in C! (However, we ll see later in the course that syntax such as new int[5] does have a meaning in C++.)
14 ENCM 339 Fall 2016 Slide Set 3 slide 14/46 Index range checking Consider this Processing code fragment: int[ ] a = new int[5]; int i; // Valid indexes are 0, 1, 2, 3, 4, but not 5. for (i = 0; i <= 5; i++) a[i] = 10 * (i + 1); An attempt to run the code will result in program termination with this error message: ArrayIndexOutOfBoundsException: 5 Let s make some notes about what happens before the error occurs, and about what causes the error.
15 ENCM 339 Fall 2016 Slide Set 3 slide 15/46 What happens with a C array? int a[5]; int i; // Valid indexes are 0, 1, 2, 3, 4, but not 5. for (i = 0; i <= 5; i++) a[i] = 10 * (i + 1); Let s make some notes about what might happen here. The fact that things might work even though they shouldn t work is not a good thing about C!
16 ENCM 339 Fall 2016 Slide Set 3 slide 16/46 C arrays and C++ vectors C programmers use array types where most current C++ programmers would use vector types. C++ vector types are generally safer than C array types, but do not exist in plain C. Here are some weaknesses of C array types relative to C++ vector types: Once a C array has been created, it cannot be resized there is no way to grow or shrink an array. With C arrays, it is usually impossible to ask for index range checking. We ll learn about C++ vectors later in ENCM 339.
17 ENCM 339 Fall 2016 Slide Set 3 slide 17/46 Outline of Slide Set 3 More about pointers as function parameters Arrays in C Brief notes about the Standard C Library Character strings in C The strcpy function and related C concepts Function parameters declared to be arrays are really pointers! Buffer overflows
18 ENCM 339 Fall 2016 Slide Set 3 slide 18/46 Brief notes about the Standard C Library In programming, a library is a collection of functions and types that is installed as part of a programming system. Programmers can use these function and types without having to define them. Some of the functions in the Processing core library are ellipse and line, used to draw things, and print and println, used to write text to the Processing Console. The Standard C Library is the set of library functions, types, and related facilities that is available on every standard-compliant implementation of C. Most C implementations also offer lots of other library features beyond what is mandated by C standards.
19 ENCM 339 Fall 2016 Slide Set 3 slide 19/46 Standard C Library Header Files Library facilities can be roughly split into categories associated with library header files files that contain lists of function prototypes, type declarations, and other related information. Header files are sometimes just called headers. Chapters 16 and 17 of C in a Nutshell (second edition), our course C textbook, list all the Standard C Library headers and summarize their contents. The next few slides say a few things about some of the most-often-used library headers.
20 ENCM 339 Fall 2016 Slide Set 3 slide 20/46 <stdio.h> std is short for standard, and io is short for input/output. printf and scanf are just two of the many input/output functions associated with <stdio.h>. Later in the course, we ll learn about types and functions that can be used to do input and output with files, instead of with a terminal window.
21 ENCM 339 Fall 2016 Slide Set 3 slide 21/46 <stdlib.h> std is short for standard, and lib is short for library. The name is confusing. <stdlib.h> provides information only about part of the Standard C Library, not the whole Standard C Library. Some of the many associated functions are exit, for forcing program termination; malloc and free, for managing dynamically allocated memory, as we ll see later in the course.
22 ENCM 339 Fall 2016 Slide Set 3 slide 22/46 <math.h> Here are a few of the many math functions... sqrt sin cos asin acos log log10 Each of the above has one parameter, of type double, and a return type of double. sqrt is short for square root. All functions related to trigonometry assume that angles are given in radians. This function is used to find the best approximation to x raised to the power y... double pow(double x, double y); For example, what would the value of pow(0.5, 3.0) be?
23 ENCM 339 Fall 2016 Slide Set 3 slide 23/46 <string.h> <string.h> are used to manipulate and answer questions about character strings, as we ll soon see.
24 ENCM 339 Fall 2016 Slide Set 3 slide 24/46 Outline of Slide Set 3 More about pointers as function parameters Arrays in C Brief notes about the Standard C Library Character strings in C The strcpy function and related C concepts Function parameters declared to be arrays are really pointers! Buffer overflows
25 ENCM 339 Fall 2016 Slide Set 3 slide 25/46 Character strings in C A string is a sequence of character codes stored in an array of char elements; the sequence is terminated by a null character. Let s demonstrate this, using a somewhat awkward way to set up a string. Important: The character constant for the null character is \0, and the value of the null character in ASCII is zero. Let s describe the algorithm used by printf when it prints a string in a response to a %s specification. Let s make diagrams for points (1) and (2) in our example program, assuming that the character set in use is ASCII.
26 ENCM 339 Fall 2016 Slide Set 3 slide 26/46 Notes on null termination 1. \0 is backslash zero, not backslash oh. 2. Terminating a string with \0 is mandatory. printf and most other string-handling functions have no other way of knowing where a string ends. 3. A string N characters in length needs at least N + 1 array elements of type char for storage. It s okay for arrays of char containing strings to be larger than necessary. 4. Null termination is used only with character strings, not with arrays that have elements of type int, double, etc.
27 ENCM 339 Fall 2016 Slide Set 3 slide 27/46 Outline of Slide Set 3 More about pointers as function parameters Arrays in C Brief notes about the Standard C Library Character strings in C The strcpy function and related C concepts Function parameters declared to be arrays are really pointers! Buffer overflows
28 ENCM 339 Fall 2016 Slide Set 3 slide 28/46 The strcpy function and related C concepts Usually programmers don t set up C strings by assigning one char at a time. A more common method is use of the strcpy ( string copy ) function in the standard library. Let s look at the example on the next slide.
29 ENCM 339 Fall 2016 Slide Set 3 slide 29/46 #include <stdio.h> #include <string.h> // library header file // for string functions int main(void) { char s[6]; strcpy(s, "foo"); // (1) printf("begin%send\n", s); return 0; } What will be in the array s at point (1)? What will be the output of the program?
30 ENCM 339 Fall 2016 Slide Set 3 slide 30/46 The function prototype for strcpy char * strcpy(char *dest, const char *src); dest has type pointer-to-char. src has type pointer-to-const-char. In the call to strcpy in main on the previous slide, the argument types match the types in the function prototype. For C beginners, these type matches are not at all obvious. Let s make a short list of things that need to be explained.
31 ENCM 339 Fall 2016 Slide Set 3 slide 31/46 An array name often generates a pointer In C and C++, in most expressions, the name of an array gets treated as the address of element 0 of the array. For example, strcpy(s, "foo"); really means strcpy(&s[0], "foo"); That is not obvious. If you are new to C, you can t know that by reading the code you need to have it explained to you! That s really means &s[0] in this context reflects an important design decision made when C was first created.
32 ENCM 339 Fall 2016 Slide Set 3 slide 32/46 String constants usually generate pointers This was another important C language design decision. A string constant occupies an array of chars located in a region of program memory we will call static storage. In this example... strcpy(s, "foo");... the second argument is actually the address of the f character at the beginning of an array in static storage. (There s an exception to this rule: String constants used as initializers for arrays of chars do not generate addresses. We ll worry about that later.)
33 ENCM 339 Fall 2016 Slide Set 3 slide 33/46 A definition for strcpy To illustrate concepts introduced the last several slides, let s pretend that strcpy is not provided in the C library, and that we have to supply our own definition. To understand where the arrays are and which addresses go into the parameters of strcpy, let s make a diagram for point (1) on the next slide. Then let s complete the definition of strcpy, and make some notes about how it will work.
34 ENCM 339 Fall 2016 Slide Set 3 slide 34/46 char *strcpy(char *dest, const char *src) { int i; // (1) } // Need some code here! int main(void) { char s[6]; strcpy(s, "hello"); return 0; }
35 ENCM 339 Fall 2016 Slide Set 3 slide 35/46 Use of square brackets and indexes with pointers The definition of strcpy involved expressions such as src[i] and dest[i]. Why do these expressions make sense? What do they really mean? Let s make some notes.
36 ENCM 339 Fall 2016 Slide Set 3 slide 36/46 The return value of strcpy Understanding how strcpy uses its parameters is really, really important! In contrast, the return value from strcpy is a minor detail. strcpy returns the address of the beginning of the destination array. That allows nested calls, such as in this fragment: char a[5], b[5]; strcpy(b, strcpy(a, "ENCM")); The string constant is copied into the array a, and then the string is copied from array a into array b. In the vast majority of calls to strcpy, the return value is not used.
37 ENCM 339 Fall 2016 Slide Set 3 slide 37/46 Use of const with pointers Consider this example: void f(const int *p, int n) { code } Here, f is not allowed to change the value of any int it might access through the pointer p. Let s add some detail to this explanation.
38 ENCM 339 Fall 2016 Slide Set 3 slide 38/46 Outline of Slide Set 3 More about pointers as function parameters Arrays in C Brief notes about the Standard C Library Character strings in C The strcpy function and related C concepts Function parameters declared to be arrays are really pointers! Buffer overflows
39 ENCM 339 Fall 2016 Slide Set 3 slide 39/46 Function parameters declared to be arrays are really pointers! This is yet another non-obvious aspect of the array/pointer relationship. Let s look at the code on the next slide, and make some diagrams and some remarks.
40 double average(const double y[ ], int n); int main(void) { double x[ ] = { 1.1, 1.2, 1.6 }, avg; avg = average(x, 3); return 0; } double average(const double y[ ], int n) { double sum = 0.0; int i; for (i = 0; i < n; i++) sum += y[i]; // (1) (After the for loop is done.) } return sum / n;
41 ENCM 339 Fall 2016 Slide Set 3 slide 41/46 Outline of Slide Set 3 More about pointers as function parameters Arrays in C Brief notes about the Standard C Library Character strings in C The strcpy function and related C concepts Function parameters declared to be arrays are really pointers! Buffer overflows
42 ENCM 339 Fall 2016 Slide Set 3 slide 42/46 Buffer overflows Here is a definition for strcpy, seen previously... char * strcpy(char *dest, const char *src) { int i; for (i = 0; src[i]!= \0 ; i++) dest[i] = src[i]; dest[i] = \0 ; return dest; } What will happen if the destination array is not big enough to hold the source string?
43 ENCM 339 Fall 2016 Slide Set 3 slide 43/46 Buffer is an informal term often used to mean array of chars used to hold some program input. The C library function gets ( get string ) which you should NEVER USE is completely defenseless against buffer overflows... #include <stdio.h> int main(void) { char name[8]; printf("hi, what is your name?\n"); gets(name); printf("nice to meet you, %s.\n", name); return 0; } Let s make some notes about what this program might do.
44 ENCM 339 Fall 2016 Slide Set 3 slide 44/46 How bad are buffer overflows? In a small program that only you and perhaps a few trusted co-workers use... Buffer overflows can cause mysterious program crashes or wrong program output. In a program that lets untrustworthy users provide input, such as software running on a Web server... BUFFER OVERFLOWS CAN BE EXTREMELY BAD. Sometimes an attacker can carefully craft a too-long input string that lets the attacker break in and take over a computer! (How this works is not an ENCM 339 topic, but there are lots of articles about it on the Web.)
45 ENCM 339 Fall 2016 Slide Set 3 slide 45/46 Avoiding buffer overflows in small C programs Write code that can detect potential buffer overflows before they happen. To do this, you must carefully read documentation of library functions, and carefully think about string-handling functions you write yourself. Example DOs and DON Ts: Don t use strcpy (or the related strcat function) unless you are certain about all of the array sizes and possible string lengths in a program. Do use safer functions for copying strings. Don t use gets to read a line of input text. Do use fgets, which is safer. Do make a plan for what a program should do if an array is too small for an input string shutting down with an error message is often a good choice.
46 ENCM 339 Fall 2016 Slide Set 3 slide 46/46 Avoiding buffer overflows in production code If software you re developing must defend itself against attackers, your development team needs experts on computer security. That level of expertise is well beyond what can be taught in a course like ENCM 339.
Slide 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 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 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 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 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 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 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng
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 ENCM 339 Fall 2017 Section 01 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 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 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 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 information8. Characters, Strings and Files
REGZ9280: Global Education Short Course - Engineering 8. Characters, Strings and Files Reading: Moffat, Chapter 7, 11 REGZ9280 14s2 8. Characters and Arrays 1 ASCII The ASCII table gives a correspondence
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 informationLecture 04 Introduction to pointers
Lecture 04 Introduction to pointers A pointer is an address in the memory. One of the unique advantages of using C is that it provides direct access to a memory location through its address. A variable
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 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 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 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 informationStrings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb
CS 201 String Debzani Deb Strings(2) Two interpretations of String Arrays whose elements are characters. Pointer pointing to characters. Strings are always terminated with a NULL characters( \0 ). C needs
More informationSlide Set 15 (Complete)
Slide Set 15 (Complete) for ENCM 339 Fall 2017 Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary November 2017 ENCM 339 Fall 2017
More informationInteger Multiplication and Division
Integer Multiplication and Division for ENCM 369: Computer Organization Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 208 Integer
More informationhttps://www.eskimo.com/~scs/cclass/notes/sx8.html
1 de 6 20-10-2015 10:41 Chapter 8: Strings Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character
More informationINTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from
INTRODUCTION TO C++ FUNCTIONS Original slides are from http://sites.google.com/site/progntut/ Dept. of Electronic Engineering, NCHU Outline 2 Functions: Program modules in C Function Definitions Function
More informationPointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables
Chapter 11 Pointers The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into bytes, with each byte capable
More informationOutline. Computer programming. Debugging. What is it. Debugging. Hints. Debugging
Outline Computer programming Debugging Hints Gathering evidence Common C errors "Education is a progressive discovery of our own ignorance." Will Durant T.U. Cluj-Napoca - Computer Programming - lecture
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 informationC: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16
Department of Computer Science College of Engineering Boise State University September 11, 2017 1/16 1-dimensional Arrays Arrays can be statically declared in C, such as: int A [100]; The space for this
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 informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationTechnical Questions. Q 1) What are the key features in C programming language?
Technical Questions Q 1) What are the key features in C programming language? Portability Platform independent language. Modularity Possibility to break down large programs into small modules. Flexibility
More informationBil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7
Strings and Clases BIL104E: Introduction to Scientific and Engineering Computing Lecture 7 Manipulating Strings Scope and Storage Classes in C Strings Declaring a string The length of a string Copying
More informationgcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world
alun@debian:~$ gcc hello.c alun@debian:~$ a.out Hello, world alun@debian:~$ gcc -o hello hello.c alun@debian:~$ hello Hello, world alun@debian:~$ 1 A Quick guide to C for Networks and Operating Systems
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 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 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 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 informationLecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch
Lecture 3 CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions Review Conditions: if( ) / else switch Loops: for( ) do...while( ) while( )... 1 Examples Display the first 10
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 04 Programs with IO and Loop We will now discuss the module 2,
More informationC Functions. 5.2 Program Modules in C
1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking
More informationComputer Science & Engineering 150A Problem Solving Using Computers
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Stephen Scott (Adapted from Christopher M. Bourke) 1 / 41 Fall 2009 Chapter 3 3.1 Building Programs from Existing Information
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 informationC: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36
Department of Computer Science College of Engineering Boise State University August 25, 2017 1/36 Pointers and Arrays A pointer is a variable that stores the address of another variable. Pointers are similar
More informationLecture 04 FUNCTIONS AND ARRAYS
Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions
More informationComputer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Functions Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Chapter 3 3.1 Building
More informationComputer Programming: Skills & Concepts (CP) Strings
CP 14 slide 1 Tuesday 31 October 2017 Computer Programming: Skills & Concepts (CP) Strings Ajitha Rajan Tuesday 31 October 2017 Last lecture Input handling char CP 14 slide 2 Tuesday 31 October 2017 Today
More informationC Programs: Simple Statements and Expressions
.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar.. C Programs: Simple Statements and Expressions C Program Structure A C program that consists of only one function has the following
More informationAgenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011
CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture
More informationPointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017
Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2012 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. Arrays: Example Syntax type name[size];
More informationWelcome to... CS113: Introduction to C
Welcome to... CS113: Introduction to C Instructor: Erik Sherwood E-mail: wes28@cs.cornell.edu Course Website: http://www.cs.cornell.edu/courses/cs113/2005fa/ The website is linked to from the courses page
More informationChapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses
Chapter 16 Pointers and Arrays Based on slides McGraw-Hill Additional material 200/2005 Lewis/Martin Pointers and Arrays We've seen examples of both of these in our LC- programs; now we'll see them in
More informationTutorial 1 C Tutorial: Pointers, Strings, Exec
TCSS 422: Operating Systems Institute of Technology Spring 2017 University of Washington Tacoma http://faculty.washington.edu/wlloyd/courses/tcss422 Tutorial 1 C Tutorial: Pointers, Strings, Exec The purpose
More informationCS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files
CS113: Lecture 7 Topics: The C Preprocessor I/O, Streams, Files 1 Remember the name: Pre-processor Most commonly used features: #include, #define. Think of the preprocessor as processing the file so as
More informationOverview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings
CPE 101 slides based on UW course Lecture 19: Strings Overview Concepts this lecture String constants ull-terminated array representation String library String initializers Arrays of strings
More informationMultiple Choice Questions ( 1 mark)
Multiple Choice Questions ( 1 mark) Unit-1 1. is a step by step approach to solve any problem.. a) Process b) Programming Language c) Algorithm d) Compiler 2. The process of walking through a program s
More informationENCM 501 Winter 2015 Tutorial for Week 5
ENCM 501 Winter 2015 Tutorial for Week 5 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary 11 February, 2015 ENCM 501 Tutorial 11 Feb 2015 slide
More informationcs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).
cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion
More informationFor example, let s say we define an array of char of size six:
Arrays in C++ An array is a consecutive group of memory locations that all have the same name and the same type. To refer to a particular location, we specify the name and then the positive index into
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 informationArrays and Pointers. CSE 2031 Fall November 11, 2013
Arrays and Pointers CSE 2031 Fall 2013 November 11, 2013 1 Arrays l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. 2 Arrays: Example
More informationCharacters, Character Strings, and string-manipulation functions in C
Characters, Character Strings, and string-manipulation functions in C see Kernighan & Ritchie Section 1.9, Appendix B3 Characters Printable characters (and some non-printable ones) are represented as 8-bit
More informationCS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor
CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static
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 informationSlide Set 11. for ENCM 369 Winter 2015 Lecture Section 01. Steve Norman, PhD, PEng
Slide Set 11 for ENCM 369 Winter 2015 Lecture Section 01 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 2015 ENCM 369 W15 Section
More informationPointers, Arrays, and Strings. CS449 Spring 2016
Pointers, Arrays, and Strings CS449 Spring 2016 Pointers Pointers are important. Pointers are fun! Pointers Every variable in your program has a memory location. This location can be accessed using & operator.
More informationJTSK Programming in C II C-Lab II. Lecture 3 & 4
JTSK-320112 Programming in C II C-Lab II Lecture 3 & 4 Xu (Owen) He Spring 2018 Slides modified from Dr. Kinga Lipskoch Planned Syllabus The C Preprocessor Bit Operations Pointers and Arrays (Dynamically
More informationBSM540 Basics of C Language
BSM540 Basics of C Language Chapter 4: Character strings & formatted I/O Prof. Manar Mohaisen Department of EEC Engineering Review of the Precedent Lecture To explain the input/output functions printf()
More informationPreview from Notesale.co.uk Page 2 of 79
COMPUTER PROGRAMMING TUTORIAL by tutorialspoint.com Page 2 of 79 tutorialspoint.com i CHAPTER 3 Programming - Environment Though Environment Setup is not an element of any Programming Language, it is the
More informationARRAYS(II Unit Part II)
ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful
More informationIntroduction to C Language
Introduction to C Language Instructor: Professor I. Charles Ume ME 6405 Introduction to Mechatronics Fall 2006 Instructor: Professor Charles Ume Introduction to C Language History of C Language In 1972,
More informationArrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.
Arrays Arrays and Pointers l Grouping of data of the same type. l Loops commonly used for manipulation. l Programmers set array sizes explicitly. EECS 2031 Fall 2014 November 11, 2013 1 2 Arrays: Example
More informationDynamic Allocation in C
Dynamic Allocation in C C Pointers and Arrays 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically,
More informationLecture07: Strings, Variable Scope, Memory Model 4/8/2013
Lecture07: Strings, Variable Scope, Memory Model 4/8/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C 1 Outline Review pointers New: Strings New: Variable Scope (global vs. local variables)
More informationString constants. /* Demo: string constant */ #include <stdio.h> int main() {
Strings 1 String constants 2 /* Demo: string constant */ #include s1.c int main() { printf("hi\n"); } String constants are in double quotes A backslash \ is used to include 'special' characters,
More informationDAY 3. CS3600, Northeastern University. Alan Mislove
C BOOTCAMP DAY 3 CS3600, Northeastern University Slides adapted from Anandha Gopalan s CS132 course at Univ. of Pittsburgh and Pascal Meunier s course at Purdue Memory management 2 Memory management Two
More informationFunctions. Systems Programming Concepts
Functions Systems Programming Concepts Functions Simple Function Example Function Prototype and Declaration Math Library Functions Function Definition Header Files Random Number Generator Call by Value
More informationRecitation: C Review. TA s 20 Feb 2017
15-213 Recitation: C Review TA s 20 Feb 2017 Agenda Logistics Attack Lab Conclusion C Assessment C Programming Style C Exercise Cache Lab Overview Appendix: Valgrind Clang / LLVM Cache Structure Logistics
More informationBIL 104E Introduction to Scientific and Engineering Computing. Lecture 4
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4 Introduction Divide and Conquer Construct a program from smaller pieces or components These smaller pieces are called modules Functions
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 informationC Functions. CS 2060 Week 4. Prof. Jonathan Ventura
CS 2060 Week 4 1 Modularizing Programs Modularizing programs in C Writing custom functions Header files 2 Function Call Stack The function call stack Stack frames 3 Pass-by-value Pass-by-value and pass-by-reference
More informationDr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)
Lecture 9 Functions Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To understand how to construct programs modularly
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 informationData Structure Series
Data Structure Series This series is actually something I started back when I was part of the Sweet.Oblivion staff, but then some things happened and I was no longer able to complete it. So now, after
More informationArrays and Pointers (part 1)
Arrays and Pointers (part 1) CSE 2031 Fall 2010 17 October 2010 1 Arrays Grouping of data of the same type. Loops commonly used for manipulation. Programmers set array sizes explicitly. 2 1 Arrays: Example
More informationCS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays
CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays C Arrays This handout was written by Nick Parlante and Julie Zelenski. As you recall, a C array is formed by laying out all the elements
More informationSo far, system calls have had easy syntax. Integer, character string, and structure arguments.
Pointers Page 1 So far, system calls have had easy syntax Wednesday, September 30, 2015 10:45 AM Integer, character string, and structure arguments. But this is not always true. Today, we begin to explore
More informationPERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur
PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam-613 403 Thanjavur 01. Define program? 02. What is program development cycle? 03. What is a programming language? 04. Define algorithm? 05. What
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 informationArray Initialization
Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]
More informationAN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University
AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:
More informationDynamic Allocation in C
Dynamic Allocation in C 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically, as the program
More informationComputer Science & Engineering 150A Problem Solving Using Computers
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using
More informationComputer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: C and Unix Overview
Computer Science 322 Operating Systems Mount Holyoke College Spring 2010 Topic Notes: C and Unix Overview This course is about operating systems, but since most of our upcoming programming is in C on a
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 informationC: How to Program. Week /Apr/23
C: How to Program Week 9 2007/Apr/23 1 Review of Chapters 1~5 Chapter 1: Basic Concepts on Computer and Programming Chapter 2: printf and scanf (Relational Operators) keywords Chapter 3: if (if else )
More informationCS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017
CS 137 Part 6 ASCII, Characters, Strings and Unicode November 3rd, 2017 Characters Syntax char c; We ve already seen this briefly earlier in the term. In C, this is an 8-bit integer. The integer can be
More informationMemory, Arrays & Pointers
1 Memory, Arrays & Pointers Memory int main() { char c; int i,j; double x; c i j x 2 Arrays Defines a block of consecutive cells int main() { int i; int a[3]; i a[0] a[1] a[2] Arrays - the [ ] operator
More informationSlide Set 1. for ENEL 339 Fall 2014 Lecture Section 02. Steve Norman, PhD, PEng
Slide Set 1 for ENEL 339 Fall 2014 Lecture Section 02 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2014 ENEL 353 F14 Section
More information