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

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

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Array Initialization

8. Characters, Strings and Files

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

by Pearson Education, Inc. All Rights Reserved.

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

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

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

Advanced Pointer & Data Storage

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

1 Pointer Concepts. 1.1 Pointer Examples

Computer Programming: Skills & Concepts (CP) Strings

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

C Characters and Strings

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

ONE DIMENSIONAL ARRAYS

CYSE 411/AIT681 Secure Software Engineering Topic #12. Secure Coding: Formatted Output

2/9/18. CYSE 411/AIT681 Secure Software Engineering. Readings. Secure Coding. This lecture: String management Pointer Subterfuge

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

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

Computers Programming Course 11. Iulian Năstac

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

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

gcc hello.c a.out Hello, world gcc -o hello hello.c hello Hello, world

Arrays, Strings, & Pointers

OBJECT-ORIENTED PROGRAMMING CONCEPTS-CLASSES II

Lecture 04 Introduction to pointers

Dynamic memory allocation (malloc)

Computers Programming Course 10. Iulian Năstac

Arrays and Pointers. CSE 2031 Fall November 11, 2013

CSC 2400: Computer Systems. Arrays and Strings in C

C-LANGUAGE CURRICULAM

Chapter 14 - Advanced C Topics

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

CSE 12 Spring 2016 Week One, Lecture Two

Tutorial 1 C Tutorial: Pointers, Strings, Exec

CSC 2400: Computer Systems. Arrays and Strings in C

PRINCIPLES OF OPERATING SYSTEMS

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

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

Computer Programming

Chapter 8 - Characters and Strings

Arrays and Pointers (part 2) Be extra careful with pointers!

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

Lecture 05 Pointers ctd..

Characters, Character Strings, and string-manipulation functions in C

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

Lectures 5-6: Introduction to C

Arrays and Pointers (part 2) Be extra careful with pointers!

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

Create a Program in C (Last Class)

C-String Library Functions

CS240: Programming in C. Lecture 14: Errors

Slide Set 3. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Multidimension array, array of strings

Intermediate Programming, Spring 2017*

Chapter 8: Character & String. In this chapter, you ll learn about;

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

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

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

C & Data Structures syllabus

M1-R4: Programing and Problem Solving using C (JULY 2018)

Introduction to C/C++ Lecture 5 - String & its Manipulation

Understanding Pointers

Memory, Arrays & Pointers

Introduction to string


COMP1917: 09 Arrays and Strings

CSE 12 Spring 2018 Week One, Lecture Two

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

Quick review pointer basics (KR ch )

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Course organization. Course introduction ( Week 1)

Chapter 13. Strings. Introduction

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

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

C Programming Review CSC 4320/6320

Recitation: C Review. TA s 20 Feb 2017

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

1. We have a code sequence that is potentially executed twice.

Multiple Choice Questions ( 1 mark)

CS 137 Part 6. ASCII, Characters, Strings and Unicode. November 3rd, 2017

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

C mini reference. 5 Binary numbers 12

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

C programming basics T3-1 -

UNIT III (PART-II) & UNIT IV(PART-I)

CS 222: Pointers and Manual Memory Management

C Strings. Abdelghani Bellaachia, CSCI 1121 Page: 1

Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

CSC231 C Tutorial Fall 2018 Introduction to C

CSE2301. Arrays. Arrays. Arrays and Pointers

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

SU 2017 May 11/16 LAB 2: Character and integer literals, number systems, character arrays manipulation, relational operator

Fundamentals of Programming & Procedural Programming

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Transcription:

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 array is declared on the stack segment of the memory for the program (if A is a local variable) or in the data segment if A is a global variable. Text 0x00000000 Program Binary Data Global/Static variables Heap Dynamically allocated variables Stack Local variables, function/method arguments 0xFFFFFFFF 2/16

Arrays in C do not contain a length property like in Java When arrays are passed as a function argument they decay to a pointer. Thus the code below COULD be incorrect if the array below is function parameter. int numofelefments = sizeof ( array ) / sizeof ( array [0]) ; Here is a good stackoverflow.com posting if you need more info https://stackoverflow.com/questions/33523585/ how-do-sizeofarr-sizeofarr0-work 3/16

Two-dimensional Arrays A 2-dimensional array can be statically allocated in C as shown in the following example: int Z [4][10]; This array is laid out in memory in row major order, that is, it is laid out as a 1d array with row 0 first followed by row 1, row 2 and then row 3. Z 0 1 2 3 4 5 6 7 8 9 0 1 2 3 row 0 row 1 row 2 row 3 Row Major Layout Some languages use a column major layout for 2-d arrays, which is column 0, then column 1,..., and finally column 9 in the above example. 4/16

C-style Strings A C-style string is an array of characters terminated by the null character - '\0' (ASCII code = 0). There are four ways to declare a string. Modifiable, fixed size array. Compiler determines size based on string literal (e.g. magma takes 6 chars). Use this if you know the length or value of the string at compile-time. char s0 [] = " magma "; s0 [0] = 'd'; /* legal */ s0 = " magma "; /* illegal */ Pointer to un-named, static, read-only array. char *s1 = " volcano "; s1 [0] = 'm'; /* illegal - read only */ s1 = " lava "; /* legal */ Empty, fixed size array. char s2[ MAXLEN ]; Uninitialized pointer. Use this if you don t know the length of the potential string until run-time. char *s3; /* Some later point in your program... */ s3 = ( char *) malloc ( sizeof ( char ) * ( strlen (s0) + 1)); 5/16

C-style Strings C doesn t provide as a basic type so we use functions to do operations with... The header file is string.h See man page for string for a list of all C string functions. Common string manipulation functions: strlen(), strcat(), strncat(), strcpy(), strncpy(), strcmp(), strncmp(), strtok(), strsep(), strfry() etc. Copying. What is wrong with the following? Read man 3 strcpy. strcpy (dest, src ); /* not safe */ We can solve this using strncpy() strncpy ( dest, src, MAXLEN ); /* safer, but read man page */ Better solution is to allocate correct size, then copy. char * dest = ( char *) malloc ( sizeof ( char )*( strlen ( src ) +1) ); strcpy ( dest, src ); 6/16

String Copy Example /* strcpy ; copy t to s, array version */ void strcpy ( char *s, char * t) { int i =0; while ((s[i] = t[i])!= '\0 ') i ++; /* strcpy ; copy t to s, pointer version 1 */ void strcpy ( char *s, char * t) { int i =0; while ((* s = *t)!= '\0 '){ s ++; t ++; 7/16

String Copy Example (contd.) /* strcpy ; copy t to s, pointer version 2 */ void strcpy ( char *s, char * t) { while ((* s++ = *t ++)!= '\0 ') ; /* strcpy ; copy t to s, pointer version 3 */ void strcpy ( char *s, char * t) { while ((* s++ = *t ++) ) ; Recommend the use of parentheses around assignment used as a boolean 8/16

String Comparison strcmp(s, t) returns negative, zero or positive if s is lexicographically less, equal or greater than t. The value is obtained by subtracting the characters at the first position where s and t differ. /* strcmp : return <0 if s<t, 0 if s==t, >0 if s>t */ int strcmp ( char *s, char * t) { int i; for ( i = 0; s[ i] == t[ i]; i ++) if (s[i] == '\0 ') return 0; return s[ i] - t[ i]; 9/16

String Comparison (contd.) The pointer version of strcmp: /* strcmp : return <0 if s<t, 0 if s==t, >0 if s>t */ int strcmp ( char *s, char * t) { for ( ; *s == *t; s++, t ++) if (*s == '\0 ') return 0; return *s - *t; 10/16

String Examples A String Example: C-examples//-ex1.c A String Tokenizing Example: C-examples//-ex2.c A Better String Tokenizing Example: C-examples//-ex3.c 11/16

Other String Tokenizing Functions Use strsep() in cases where there are empty fields between delimiters that strtok() cannot handle. 12/16

Command line arguments Recommended prototype for the main function: int main ( int argc, char * argv []) or int main ( int argc, char ** argv ) argc C-style are being passed to the main function from argv[0] through argv[argc-1]. The name of the executable is argv[0], the first command line argument is argv[1] and so on. Thus argv is an array of pointers to char. In-class Exercise. Draw the memory layout of the argv array. 13/16

Variable Argument Lists in C (1) C allows a function call to have a variable number of arguments with the variable argument list mechanism. Use ellipsis... to denote a variable number of arguments to the compiler. the ellipsis can only occur at the end of an argument list. Here are some standard function calls that use variable argument lists. int printf ( const char * format,...) ; int scanf ( const char * format,...) ; int execlp ( const char * file, const char * arg,...) ; See man stdarg for documentation on using variable argument lists. In particular, the header file contains a set of macros that define how to step through the argument list. See Section 7.3 in the K&R C book. 14/16

Variable Argument Lists in C (2) Useful macros from stdarg header file. va_list argptr; is used to declare a variable that will refer to each argument in turn. void va_start(va_list argptr, last); must be called once before argptr can be used. last is the name of the last variable before the variable argument list. type va_arg(va_list ap, type); Each call of va_arg returns one argument and steps ap to the next; va_arg uses a type name to determine what type to return and how big a step to take. void va_end(va_list ap); Must be called before program returns. Does whatever cleanup is necessary. It is possible to walk through the variable arguments more than once by calling va_start after va_end. 15/16

Variable Argument Lists Example /* C- examples / varargs /test - varargs.c */ # include <stdio.h> # include <stdarg.h> void strlist ( int n,...) { va_list ap; char *s; va_start (ap, n); while (1) { s = va_arg (ap, char *); printf ("%s\n",s); n - -; if (n==0) break ; va_end (ap); int main () { strlist (3, " string1 ", " string2 ", " string3 "); strlist (2, " string1 ", " string3 "); 16/16