Arrays Arrays and pointers Loops and performance Array comparison Strings. John Edgar 2

Similar documents
Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Assignment 1 Clarifications

CS 61c: Great Ideas in Computer Architecture

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

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Fundamental of Programming (C)

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

Procedural programming with C

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Variation of Pointers

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

PROGRAMMAZIONE I A.A. 2017/2018

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Memory, Arrays & Pointers

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

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

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

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

Multidimension array, array of strings

Chapter 16. Pointers and Arrays. Address vs. Value. Another Need for Addresses

Basic and Practice in Programming Lab7

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

ECE 15B COMPUTER ORGANIZATION

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

[0569] p 0318 garbage

SYSC 2006 C Winter 2012

Programming and Data Structures

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

First of all, it is a variable, just like other variables you studied

Fundamentals of Programming

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Pointers, Arrays, and Strings. CS449 Spring 2016

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

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

Chapter 3: Arrays and More C Functionality

Introduction to Scientific Computing and Problem Solving

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

EM108 Software Development for Engineers

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

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

C++ ARRAYS POINTERS POINTER ARITHMETIC. Problem Solving with Computers-I

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

Operators And Expressions

Dynamic Allocation in C

Systems Programming and Computer Architecture ( )

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

Principles of C and Memory Management

BSM540 Basics of C Language

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Principles of C and Memory Management

Lecture 3. More About C

8. Characters, Strings and Files

Functions. Arash Rafiey. September 26, 2017

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

CSE2301. Arrays. Arrays. Arrays and Pointers

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Goals of this Lecture

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

C-Refresher: Session 06 Pointers and Arrays

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Computer Programming Unit 3

Programming in C++: Assignment Week 1

Fundamentals of Programming Session 20

High Performance Programming Programming in C part 1

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

Today s lecture. Continue exploring C-strings. Pointer mechanics. C arrays. Under the hood: sequence of chars w/ terminating null

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Pointers, Dynamic Data, and Reference Types

Create a Program in C (Last Class)

Today s lecture. Pointers/arrays. Stack versus heap allocation CULTURE FACT: IN CODE, IT S NOT CONSIDERED RUDE TO POINT.

POINTER & REFERENCE VARIABLES

War Industries Presents: An Introduction to Programming for Hackers Part III - Advanced Variables & Flow Control.

Arrays. Here is the generic syntax for an array declaration:

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

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

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

C Arrays, Strings, More Pointers Instructor: Steven Ho

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

CSCI 171 Chapter Outlines

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

More about BOOLEAN issues

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

UIC. C Programming Primer. Bharathidasan University

WARM UP LESSONS BARE BASICS

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

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

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

Administrivia. Introduction to Computer Systems. Pointers, cont. Pointer example, again POINTERS. Project 2 posted, due October 6


Transcription:

CMPT 125

Arrays Arrays and pointers Loops and performance Array comparison Strings John Edgar 2

Python a sequence of data access elements with [index] index from [0] to [len-1] dynamic length heterogeneous types has methods C a sequence of data access elements with [index] index from [0] to [len-1] fixed length homogeneous types has no methods John Edgar 4

Declare an array with its type and []s after the name int scores[4]; Then set values using an index scores[0] = 5; scores[1] = 3; scores[2] = 7; scores [3] = 4; length of the array Alternatively declare and initialize arrays in one int scores[4] = {5, 3, 7, 4; Can only be used on declaration Can not be used to set values on existing arrays John Edgar 5

If an array is not initialized it will contain garbage values The bit pattern that happens to be stored in the array elements' memory locations The sizeof function can be used to find the length of an array But only for static arrays int arr[10]; for(int i = 0; i < sizeof(arr) / sizeof(int); ++i){ printf("arr[%d] = %d\n", i, arr[i]);

Be careful not to access an array using an index that is out of bounds Less than zero or greater than array size - 1 Something undesirable will happen It might print garbage Or crash Python would generate a run-time error int arr[10] = {0,1,2,3,4,5,6,7,8,9; for(int i = 0; i <= 10; ++i){ printf("arr[%d] = %d\n", i, arr[i]);

If an array is passed to a function, changes made to it within the function will persist Because an array variable is a constant pointer to the first element of the array So an array parameter actually specifies the address of the array This is still pass by value Just that the value being passed is a pointer

int sumarray(int arr[], int size) { int sum = 0; for(int i=0; i < size; ++i){ sum += arr[i]; return sum; Since an array variable is a pointer the two function headers are essentially identical But the first makes it explicit that an array is passed to the function the function could also have been written like this int sumarray(int* arr, int size) { int sum = 0; for(int i=0; i < size; ++i){ sum += arr[i]; return sum;

void printarray(int arr[], int size) { int* p; for(p = arr; p < arr + size; p++){ printf("%d\n",*p); dereference p to access the value it points to this seems OK, assign the address of the first element of the array to p what s going on here? this correctly prints an array why?

The preceding example included this statement p++; // p is a pointer to an int This is an example of pointer arithmetic The statement looks like it should add one to the address that p stores Making it point to an address that doesn t match a variable However it does not do this, instead it adds the size of an int to the address stored in p i.e. 4 This is another example of operations that behave differently based on operand type

Pointer arithmetic int arr[] = {1,2,3,4; int* p = arr; variable type address arr constant int* 2048 p int* 2064 symbol table next free byte data 1 2 3 4 2048 address 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068... call stack

Pointer arithmetic int arr[] = {1,2,3,4; int* p = arr; p++; variable type address arr constant int* 2048 p int* 2064 symbol table it looks like this should add one to the value of p, making it point to the int at address 2049 next free byte data 1 2 3 4 2048 2049 address 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068... call stack Fortunately this is not what happens

Pointer arithmetic int arr[] = {1,2,3,4; int* p = arr; p++; variable type address arr constant int* 2048 p int* 2064 symbol table instead, the size of the type of data that p points to is added to p, making it point to the next int next free byte data 1 2 3 4 2048 2052 address 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068... call stack

Here is another example of using pointer arithmetic to iterate through an array int sumarray2(int* start, int* end) { int sum = 0; the function would be called like this while(start < end){ sum += *start++; sumarray2(arr2, arr2 + ARR_SIZE) return sum; dereferences start, adds the value to sum then increments start Note that I don t see any real value in writing the function like this, and it is fairly hard to understand unless you have a good knowledge of pointers Note the interesting precedence rules, 1 (size of an int) is added to start, not to the variable pointed to by start

Assignment, e.g. p = &x Pointer type should be compatible with variable Dereferencing, e.g. *p = 12 The *operator accesses the variable that is pointed to Arithmetic, e.g. ++p, p += 4, p-- The amount added to or subtracted from the address is multiplied by the size (in bytes) of the variable pointed to Differencing, e.g. int x = p q The difference in elements between the pointers i.e. the difference in the addresses of p and q, divided by the size of the variable pointed to

int arr[4]; int* p; int* q; Valid Invalid Notes p++; arr++ arr is a constant pointer, so the address stored in arr cannot be changed q = p + 2; p = p + q; pointer arithmetic allows integers to be added to pointers, but does not allow pointers to be added together

Python iteration for i in range (n): while <condition>: break continue C iteration for(int i=0; i < n; i++) { while (<condition>) { do{ while(<condition>); break continue The main difference in syntax is in the for loop John Edgar 20

// Prints the factorials from 1 to n int main() { long long factorial = 1; int n; printf("enter an integer: "); scanf("%d", &n); for (int i = 1; i <= n; ++i){ factorial = factorial * i; printf("%d! = %lld\n", i, factorial); The loop control statement consists of three statements initialization condition increment In this example the loop control variable is also declared in the initialization statement

Initialization condition true Loop Body Increment Rest of Program false for statements consist of three expressions Separated by ;s Initialization Executed only once Condition Tested before each iteration The last time the condition is tested there is no iteration Since the test returns false Increment Applied after each iteration

Adding an extra semi-colon for (int i = 1; i <= n; ++i); { result = result * i; printf("%d! = %lld\n", i, factorial); Forgetting opening and closing brackets for (int i = 1; i <= n; ++i) result = result * i; printf("%d! = %lld\n", i, factorial); It is good style to always use brackets even if the loop body only contains one statement empty loop body not included in loop body John Edgar 23

Python def gcd(a, b): while b!= 0: temp = b b = a % b a = temp return a C While loops in C and Python are very similar Conditions in C and Python are the same, 0 is treated as false and non-zero as true int gcd(int a, int b){ while (b!= 0) { int temp = b; b = a % b; a = temp; return a; John Edgar 24

What follows is a few lines of code but it could take a long time to run long long total = 0; for (int i = 1; i <= n; i++) { total += i; The running time depends on the value of n As n increases the running time increases If we add one to n, then the loop iterates one more time The relationship between n and running time is linear John Edgar 25

We can time programs using the Linux time command n time./a.out time 100,000,000 253 500,000,000 1,132 1,000,000,000 2,204 2,000,000,000 4,300 5000 4500 4000 3500 3000 2500 2000 1500 1000 500 0 0 500 1,000 1,500 2,000 MILLIONS John Edgar 26

int main ( ) { int password[3] = {1, 2, 3; int answer[3]; for (int i = 0; i < 3; i++) { printf("enter digit %d: ", i+1); scanf("%d", answer+i); logic error if (password!= answer) { printf("incorrect password!\n"); compares the pointer values (addresses) not the array elements John Edgar 28

A function to compare two arrays Needs to compare pairs of elements from each array int arrcompare(int arr1[], int arr2[], int length) { for (int i = 0; i < length; i++) { if (arr1[i] < arr2[i]) { return -1; else if (arr1[i] > arr2[i]) { return 1; return 0; John Edgar 29

In C a string is just an array of characters A char is a single byte That stores an ASCII code An array of characters forms a string The end of the string is marked with a null character '\0' or the ASCII code 0 So the array must be large enough to hold all of the characters plus one John Edgar 31

It s easy to print the ASCII code for a character char ch = 'x'; printf("code for %c = %d", ch, ch); The first placeholder prints the letter that the code represents The second placeholder prints the code C will also allow arithmetic to be performed on char variables The underlying numeric codes are operated on

Let s say that we want to print all of the letters from A to Z We could write 26 printf statements printf('a'); printf('b');... Or we could do this char ch = 'A'; while(ch < 'A' + 26){ printf("%c\n", ch); ch++;

int main() { char name[20]; int age; things to note printf("what is your name? "); scanf("%s", name); printf("what is your age? "); scanf("%d", &age); printf("your name is %s, and your age is %d\n", name, age); return 0; What is your name? Jenny What is your age? 11 Your name is Jenny, and your age is 11

The line char name[20]; declares an array of 20 characters A sequence in main memory with enough space for twenty characters An array is an ordered sequence of data elements of one type The brackets identify name as an array rather than a single character And 20 indicates the size of the array

... J e n n y \0 A sequence of 2o adjacent bytes in main memory Why 20 bytes? Because each character is stored in one byte What 's the \0 in the 6 th. byte? A null character to indicate the end of the string Why is this necessary? Because memory locations can t be empty, so it is important to distinguish between characters we want to store and garbage values

A string consists of an array containing the words in the string and the null character Consequently, a string containing a single character is not the same as a char variable the character 'a' the string "a" a \0 The type of a character array is not the same as the type of a character That is char name[20] declares an array not a char a

What gets printed? The difference is the null character #define BATMAN "Bruce Wayne" int main() { printf("size of %s = %d\n", BATMAN, strlen(batman)); printf("batman size = %d\n\n", sizeof(batman)); return 0;

#include <stdio.h> #include <string.h> string functions int main ( ) { char password[4] = "abc"; char answer[4]; printf("enter 3 character code: "); scanf("%s", answer); no & if (strcmp(password, answer)!= 0) { printf("incorrect password!\n"); returns 0 if the same; < 0 if first < last; >0 if first > last John Edgar 39

void strcpy(char dest[], char src[]) copies source to destination The variable name is not preceded by an & void strcat(char dest[], char src[]) appends source to destination Actual function header in libraries may differ Both these functions are potentially unsafe Why?