CSC 2400: Computer Systems. Arrays and Strings in C

Similar documents
CSC 2400: Computer Systems. Arrays and Strings in C

Variables, Pointers, and Arrays

C programming basics T3-1 -

Arrays, Strings, & Pointers

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Introduction to string

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

ONE DIMENSIONAL ARRAYS

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

C Fundamentals! C vs. Java: Design Goals. q Java design goals! q Implications for Java! language, machine language, hardware!

Array Initialization

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

20 Dynamic allocation of memory: malloc and calloc

Contents. A Review of C language. Visual C Visual C++ 6.0

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

UNIT 6. STRUCTURED DATA TYPES PART 1: ARRAYS

211: Computer Architecture Summer 2016

8. Characters, Strings and Files

C-String Library Functions

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

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

ARRAYS(II Unit Part II)

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Create a Program in C (Last Class)

Computers Programming Course 10. Iulian Năstac

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Programming: Skills & Concepts (CP) Strings

Understanding Pointers

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Lecture 05 Pointers ctd..

C Characters and Strings

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

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

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

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

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

Arrays and Pointers in C. Alan L. Cox

CSE2301. Arrays and Pointers. These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University. Arrays

CSE2301. Arrays. Arrays. Arrays and Pointers

Procedural Programming & Fundamentals of Programming

Model Viva Questions for Programming in C lab

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

Multiple Choice Questions ( 1 mark)

Chapter 8 C Characters and Strings

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

Fundamentals of Programming & Procedural Programming

COMP1917: 09 Arrays and Strings

Chapter 8 Character Arrays and Strings

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

String constants. /* Demo: string constant */ #include <stdio.h> int main() {

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

PRINCIPLES OF OPERATING SYSTEMS

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Summary of Last Class. Processes. C vs. Java. C vs. Java (cont.) C vs. Java (cont.) Tevfik Ko!ar. CSC Systems Programming Fall 2008

PDS Class Test 2. Room Sections No of students

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

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

CSC 1600 Memory Layout for Unix Processes"

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

ESC101N: Fundamentals of Computing End-sem st semester

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

Announcements. Strings and Pointers. Strings. Initializing Strings. Character I/O. Lab 4. Quiz. July 18, Special character arrays

by Pearson Education, Inc. All Rights Reserved.

Characters and Strings

Week 8 Lecture 3. Finishing up C

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

Arrays and Pointers (part 1)

Lectures 5-6: Introduction to C

This is CS50. Harvard University Fall Quiz 0 Answer Key

arrays and strings week 3 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 22 imperative programming review

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Dynamic memory allocation (malloc)

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

Fundamentals of Programming

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

CS 261 Fall Mike Lam, Professor. Structs and I/O

CS 322 Operating Systems Practice Midterm Questions

Course organization. Course introduction ( Week 1)

Lecture 04 Introduction to pointers

Fundamental of Programming (C)

Procedural Programming & Fundamentals of Programming

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

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

Memory, Arrays & Pointers

C Programming Basics II

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

Pointers and File Handling

Basic Buffer Overflows

COP 3223 Final Review

Lectures 5-6: Introduction to C

Principles of C and Memory Management

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

CSC 270 Survey of Programming Languages. C-String Values

mith College Computer Science CSC352 Week #7 Spring 2017 Introduction to C Dominique Thiébaut

C Programming Language Review and Dissection III

Transcription:

CSC 2400: Computer Systems Arrays and Strings in C Lecture Overview Arrays! List of elements of the same type Strings! Array of characters ending in \0! Functions for manipulating strings 1

Arrays in C Label Address int a[5]; a 217 a[0] a[1] 400 404 a[2] 408 226 a[3] 412 a[4] 416 What is a in the picture above? a is the address of the first array element a[0] - not five consecutive array elements - we will see that a is a constant pointer (covered in next lecture) Array Indices Logically, valid indices for an array range from 0 to MAX-1, where MAX is the dimension of the array int a[6]; stands for a[0], a[1], a[2], a[3], a[4] and a[5] Logically, there is no a[6]! Memory a[0] a[1] a[2] a[3] a[4] a[5] 2

Arrays: C vs. Java Arrays Java int [] a = new int [10]; float [][] b = new float [5][20]; C int a[10]; float b[5][20]; Array bound checking // run-time check /* no run-time check */ C Does Not Do Bounds Checking! int a[5]; a 55 217 Label a[0] Address 400 a[0] = 217; a[3] = 226; 226 a[1] a[2] a[3] 404 408 412 a[4] 416 a[-1] = 55; a[7] = 320; 320 Unpleasant if you happened to have another variable before the array variable a, or after it! 3

Example Program: Reverse Array Reverse the values in an array! Inputs: integer array a, and number of elements n! Output: values of a stored in reverse order Algorithm! Swap the first and last elements in the array! Swap the second and second-to-last elements! 77 31 94 5 186 Example of Array Code void reverse (int a[], int n) { int l, r, temp; for (l=0, r=n-1; l<r; l++, r--) { temp = a[l]; a[l] = a[r]; a[r] = temp; int main(void) { int fib[] = {1,2,3,4,5; reverse(fib, 5); 4

NO Aggregate Array Operations Aggregate operations refer to operations on an array as a whole, as opposed to operations on individual array elements. #define MAX 100 int x[max]; int y[max]; There are no aggregate operations on arrays: Assignment x = y; Error! Comparison if (x == y) Error! I/O printf( %d, x); Error! Arithmetic: x = x + y; Error! Activity Write a small program that uses aggregate array operations. What error messages do you get? 5

Stack vs. Data At run-time, memory devoted to program is divided into sections: 0000 TEXT RODATA DATA HEAP Instructions (code) Read-only data (string constants) Global variables FFFF STACK Local variables and function parameters Clobbering Example 1 /* This program accesses an invalid array cell. ** Why does it work? Draw the memory map. */ main() { int a[2]; /* 2 cells, each cell 4 bytes (32 bits) */ int b[4]; /* 4 cells, each cell 4 bytes (32 bits) */ int c[4]; /* 4 cells, each cell 4 bytes (32 bits) */ char d[5]; /* 5 cells, each cell 1 bytes (8 bits) */ a[0]=5; b[1]=4; c[0]=9; d[4]='a'; b[4]=10; printf("%d\n",b[4]); printf("%d\n",a[0]); /* Why did a[0] change? */ 6

Local Variables Are allocated on the stack The stack grows from high memory addresses towards low memory addresses. Address Label 800 804 808 812 816 820 b[0] b[1] b[2] b[3] a[0] a[1] Stack grows Clobbering Example 2 /* This program accesses an invalid array cell. ** Why does it work? Draw the memory map. */ int a[2]; /* 2 cells, each cell 4 bytes (32 bits) */ int b[4]; /* 4 cells, each cell 4 bytes (32 bits) */ int c[4]; /* 4 cells, each cell 4 bytes (32 bits) */ char d[5]; /* 5 cells, each cell 1 bytes (8 bits) */ main() { a[0]=5; b[1]=4; c[0]=9; d[4]='a'; b[4]=10; printf("%d\n",b[4]); printf("%d\n",c[0]); /* Why did c[0] change? */ 7

Global Variables Are allocated in the data section Memory in the data section is allocated from low memory addresses towards high addresses. Address Label 400 404 408 412 416 420 424 a[0] a[1] b[0] b[1] b[2] b[3] c[0] Data grows Strings in C 8

C vs. Java Strings Strings String concatenation Java String s1 = "Hello"; String s2 = new String("hello"); s1 = s1 + s2 s1 += s2 C char s1[] = "Hello"; char s2[6]; strcpy(s2, "hello"); #include <string.h> strcat(s1, s2); Strings Unlike Java, there is no String data type in C A string is just an array of characters (pointer to character), terminated by a \0 char (a null, ASCII code 0). char mystring[6] = { H, e, l, l, o, \0 ; char mystring[6] = Hello ; char mystring[] = Hello ; Equivalent mystring H e l l o \0 9

Printing a String printf( %s,mystring); mystring int i; for (i=0; mystring[i]; i++) putchar(mystring[i]); H e l l o \0 Reading Into a String Always use fgets: #define MAX_BUFFER 20 char buffer[max_buffer]; fgets(buffer, MAX_BUFFER, stdin); Avoids going past the boundary of the array 10

String Termination char mystring[] = Hello ; mystring H e \0 l x l o \0! mystring[2] = 0; equivalently, mystring[2]= \0 ; printf( %s\n,mystring); He mystring[2] = x ; mystring[5] =! ; printf( %s\n,mystring); What will happen? Functions for Manipulating Strings C provides a large number of functions for manipulating strings. Four important ones are: strlen(s) // returns the length of s strcpy(tos, froms) // copy froms to tos (tos must be large enough) strcmp(s1, s2) // returns 0 if s1 == s2 // returns an integer < 0 if s1 < s2 // returns an integer > 0 if s1 > s2 strncmp sprintf strcat read online to find out what these functions do 11

Hands-On: Understanding strlen Step 1: Write a simple test program to see how strlen behaves. Step 2: Write a function length that mimics strlen: int length (char s[]) // Input: string s terminated by \0 // Output: length of s (not counting \0 ) Do not use strlen in your code. strlen() - Computing the length of a string #include <stdio.h> #include <string.h> main() { int length; char s[6]; s[0]='s'; s[1]='u'; s[2]='e'; s[3]='\0'; length=0; while (s[length]!= '\0') length++; printf("%d\n",length); Equivalent to: length=strlen(s); 12

Hands-On: Understanding strcmp Step 1: Write a simple test program to see how strcmp behaves. Step 2: Write a function compare that mimics strcmp: int compare (char s1[], char s2[]) // Input: strings s1 and s2 terminated by \0 // Output: 0 if s1 == s2, else the difference between // the first pair of characters that do not match Do not use strcmp in your code. Hands-On: Understanding strcpy Step 1: Write a simple test program to see how strcpy behaves. Step 2: Write a function copy that mimics strcpy: void copy (char t[], char s[]) // Input: strings t(arget) and s(ource) terminated by \0 // Action: copy s into t // Assumption: t is big enough memory to hold s Do not use strcpy in your code. 13

Hands-On: Understanding strcat Step 1: Write a simple test program to see how strcat behaves. Step 2: Write a function concat that mimics strcat: void concat (char t[], char s[]) // Input: strings t(arget) and s(ource) terminated by \0 // Action: append s at the end of t // Assumption: t is big enough to hold s concatenated to t Do not use strcat in your code. sprintf() Print formatted output into a string #include <stdio.h> #include <string.h> main() { char a[24]; float f; int i; f=3.72; i=9; sprintf(a,"price %f, qty %d",f,i); printf("%s\n",a); 14

Command Line Arguments /* Print out the command line arguments ** - they are an array of strings */ int main(int argc, char *argv[]) { int i,j; for (i=0; i<argc; i++) { j=0; while (argv[i][j]!= '\0') { printf("%c",argv[i][j]); j++; printf("\n"); Equivalent to: printf("%s\n",argv[i]); Multi-Dimensional Arrays 15

2D Arrays /* How does a 2D array fit in memory? ** Draw the memory map. */ #include <stdio.h> main() { int a[3][2]; a[0][1]=7; a[1][0]=13; 3D Arrays /* How does a 3D array fit in memory? ** Draw the memory map. */ #include <stdio.h> main() { int b[2][3][4]; b[0][2][0]=7; b[1][0][2]=13; 16

Summary Arrays! Lists of elements of the same type! No bounds checking in C!!!!!!! No aggregate array operations Strings! Arrays of characters! Special end-of-string character \0! Special manipulating functions (string.h) 17