For instance, we can declare an array of five ints like this: int numbers[5];

Similar documents
printf( Please enter another number: ); scanf( %d, &num2);

However, in C we can group related variables together into something called a struct.

In the case of the dynamic array, the space must be deallocated when the program is finished with it. free(x);

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

Lectures 5-6: Introduction to C

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS 61c: Great Ideas in Computer Architecture

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Lectures 5-6: Introduction to C

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Memory and C++ Pointers

CPE 101, reusing/mod slides from a UW course (used by permission) Lecture 5: Input and Output (I/O)

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

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Bernhard Boser & Randy Katz

Variables and literals

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Agenda. Components of a Computer. Computer Memory Type Name Addr Value. Pointer Type. Pointers. CS 61C: Great Ideas in Computer Architecture

Lesson 7. Reading and Writing a.k.a. Input and Output

CS 61C: Great Ideas in Computer Architecture. Lecture 3: Pointers. Krste Asanović & Randy Katz

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Personal SE. Functions, Arrays, Strings & Command Line Arguments

Arrays. Lecture 9 COP 3014 Fall October 16, 2017

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

ESC101N: Fundamentals of Computing End-sem st semester

Arrays and Pointers in C. Alan L. Cox

Section 2.2 Your First Program in Java: Printing a Line of Text

Display Input and Output (I/O)

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

CSE101-Lec#17. Arrays. (Arrays and Functions) Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

CS 3305 Intro to Threads. Lecture 6

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CSE 12 Spring 2016 Week One, Lecture Two

Programming refresher and intro to C programming

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

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Arrays and Strings. Arash Rafiey. September 12, 2017

CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

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

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

CS 241 Data Organization Pointers and Arrays

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

DCS/100 Procedural Programming

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Kurt Schmidt. October 30, 2018

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Pointers, Dynamic Data, and Reference Types

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

CSCB09: Software Tools and Systems Programming. Announcement. The address space. Memory model

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Flow Chart. The diagrammatic representation shows a solution to a given problem.

211: Computer Architecture Summer 2016

CS61C : Machine Structures

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions


Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

(Refer Slide Time: 00:51)

High Performance Computing Prof. Matthew Jacob Department of Computer Science and Automation Indian Institute of Science, Bangalore

HW1 due Monday by 9:30am Assignment online, submission details to come

CS 31: Intro to Systems C Programming. Kevin Webb Swarthmore College September 13, 2018

Write a C program using arrays and structure

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Dynamic memory allocation

CSC 2400: Computer Systems. Arrays and Strings in C

CSE 12 Spring 2018 Week One, Lecture Two

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Separate Compilation Model

Fortunately not. In LC-3, there are a variety of addressing modes that deal with these concerns.

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Computer Science E-119 Fall Problem Set 3. Due before lecture on Wednesday, October 31

Create a Program in C (Last Class)

Pointers and Arrays A QUICK PREVIEW OF FOR CHAPTERS 10 AND 11 CMPE13. Cyrus Bazeghi

Array Initialization

Fundamentals of Programming

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

COP 3223 Final Review

CS61C : Machine Structures

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

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

Introduction to Java & Fundamental Data Types

This is CS50. Harvard University Fall Quiz 0 Answer Key

Lecture XXIV: Review Xuan Guo CSC 3210 Computer Organization and Programming Georgia State University April 23, 2015 Xuan Guo Lecture XXIV: Review

Computers Programming Course 10. Iulian Năstac

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

CSC 2400: Computer Systems. Arrays and Strings in C

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

CS 231 Data Structures and Algorithms, Fall 2016

CS 241 Data Organization Binary Trees

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

Fundamental of Programming (C)

Appendix: Common Errors

The C++ Object Lifecycle. EECS 211 Winter 2019

NCSU ECE 209 Sections 602 Exam 1 Fall September, I have neither given not received unauthorized assistance on this test.

Transcription:

CIT 593 Intro to Computer Systems Lecture #17 (11/13/12) Arrays An array is a collection of values. In C, as in many other languages: all elements of the array must be of the same type the number of elements in the array is fixed For instance, we can declare an array of five ints like this: int numbers[5]; Each element of the array can be accessed by a 0-based index (meaning, the first element has index 0). numbers[4] = 2; int x = numbers[2] + 4; Note that, unlike in Java, there is nothing in C to stop you from going outside the bounds of the array. For instance, with the five-element array declared above, both of the following are perfectly legal (though perhaps not desirable): numbers[8] = 11; numbers[-2] = 3; What is the implication of doing something like that? Let s look at this example: int even[] = { 2, 4, 6, 8, 10 }; int odd[] = { 1, 3, 5, 7, 9 }; Note the shorthand notation for declaring and initializing the elements of an array. If these were local variables, i.e. declared on the stack, they would look like this: 1 odd[0] 3 odd[1] 4 odd[2] 5 odd[3] 9 odd[4] 2 even[0] 4 even[1] 6 even[2] 8 even[3] 10 even[4] That is, the five elements of even would be put on the stack first, and on top of those would be the five elements of odd. The symbol table keeps track of the starting address (relative to the FP) of the starts of each array. So when you access odd[2], it finds the start of the odd array and then goes down two addresses to get the right spot in memory.

So, what happens when you do this? odd[6] = -1; As you can imagine, it will go down six addresses from the start of the odd array, and it will change that value, giving us this: 1 odd[0] 3 odd[1] 4 odd[2] 5 odd[3] 9 odd[4] 2 even[0] -1 even[1] 6 even[2] 8 even[3] 10 even[4] So in this case, we have overwritten the value of even[1]. Arrays and Functions There are two important rules to remember when it comes to using arrays with functions. 1. Unlike in Java, in which arrays are objects, in C there is no standard way to know the number of elements in an array. 2. Unlike primitive datatypes, which are passed by value, arrays are passed by reference. This means that, if you pass an array to a function that changes that array, the array itself is actually changed To address issue #1, it is typical in C to pass not only the array itself to a function, but also an int representing the number of elements in the array. The implication of issue #2 is that you do need to be careful about side effects. For example, here s a function that will multiply every element in the array by -1. void negate(int array[], int length) { int i = 0; for (i = 0; i < length; i++) { array[i] *= -1; } } So if you called that function like this: int numbers[] = { 1, 4, 5, 9 }; negate(numbers, 4);

Then, when the function returns, numbers[0] would be -1, numbers[1] would be -4, and so on. Two-Dimensional Arrays As in most other programming languages, you can have arrays of arrays, or what is more commonly known as two-dimensional arrays. int numbers[4][6]; numbers[2][3] = 19; Strings In C, a string is simply an array of chars. As we saw in LC-3, strings are null-terminated, meaning that the last element of the array is null, which is ASCII value 0. For instance, we can declare a string like this: char name[] = Joe ; On the stack, it would look like this: J name[0] o name[1] e name[2] 0 name[3] Note that name is a four-element char array (because of the terminating null), not three. We can print strings using the %s placeholder with printf: printf( Hello, %s.\n, name); would print Hello, Joe. Just like other arrays, we can change the individual elements: name[1] = a ; printf( Hello, %s.\n, name); would now print Hello, Jae. We can also read strings using %s with scanf: scanf( %s, &input); Keep in mind that scanf will put the terminating null at the end of whatever is entered, but it will not attempt to squeeze everything into the space allocated for the input.

For instance, if the user typed abc123, then on the stack we d have: a input[0] b input[1] c input[2] 1 input[3] 2 input[4] 3 input[5] 0 input[6]? input[7]? input[8]? input[9] In this case, input[6] would get the null value, but the last three chars would not be modified and thus would likely be unknown. But what if the user entered helloworld, which is 10 characters? Then the stack would be like this: h input[0] e input[1] l input[2] l input[3] o input[4] w input[5] o input[6] r input[7] l input[8] d input[9] 0 Other variable Note that, in this case, the null value overwrites whatever variable happened to be below input on the stack. This can lead to all sorts of trouble, as you ll see in other courses. To address this, you can specify the number of characters to read from scanf like this: scanf( %9s, &input); In this case, the %9s means only read the first nine characters. Keep in mind that you shouldn't use %10s in this case, because you need one extra space for the null in the array input.

You can determine the length of a string by using the strlen function. It counts the characters in the string up until the null, and returns the count (note that the return value does not include the null). scanf( %s, &input); if (strlen(input) > 9) PROBLEM!