the c language : arrays and stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf ·...

12
1/12 Practical Programming The C Language : The C Language : David Bouchet David Bouchet [email protected] Arrays and Strings Arrays and Strings

Upload: others

Post on 31-Dec-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

1/12

Practical Programming

The C Language :The C Language :

David BouchetDavid Bouchet

[email protected]

Arrays and StringsArrays and Strings

Page 2: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

2/12

Arrays

● An array is a collection of values that have the same type.

● The size of an array is fixed (it cannot be changed).

● An array can be either one-dimensional or multidimensional.

● The most commonly used arrays are one-dimensional and two-dimensional.

● Values are selected by integer indexes.● Indexes always start at 0.

Page 3: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

3/12

Declaring Arrays

a[0] = 0a[1] = 0a[2] = 0a[3] = 0a[4] = 4196000a[5] = 0a[6] = 4195552a[7] = 0a[8] = -1184014480a[9] = 32766----------------a[0] = 5a[1] = 5a[2] = 5a[3] = 5a[4] = 5a[5] = 5a[6] = 5a[7] = 5a[8] = 5a[9] = 5

Page 4: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

4/12

Declaring and Initializing Arrays

a[0] = 2.000000a[1] = 3.500000a[2] = 7.800000a[3] = 9.900000a[4] = 2.000000----------------b[0] = 2.000000b[1] = 3.500000b[2] = 7.800000b[3] = 9.900000b[4] = 0.000000

Page 5: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

5/12

Out of Bound Access – Reading

$ gcc -Wall -Wextra out_of_bound.c $ ./a.out a[100] = -692211237$ ./a.out a[100] = -2024418853$ ./a.out a[100] = 1341063643

No compilation errors!No compilation errors!No compilation warnings!No compilation warnings!

Undefined behavior!Undefined behavior!

out_of_bound.cout_of_bound.c

Page 6: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

6/12

Out of Bound Access – Writing

No compilation errors!No compilation errors!No compilation warnings!No compilation warnings!

Segmentation fault (access violation)!Segmentation fault (access violation)!The program crashes!The program crashes!

out_of_bound.cout_of_bound.c

$ gcc -Wall -Wextra out_of_bound.c $ ./a.out Segmentation fault (core dumped)

Page 7: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

7/12

Manipulating Arrays – Example

Average = 11.25Min = 2

Page 8: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

8/12

Two-Dimensional Arrays

mat[0][0] = 0mat[0][1] = 1mat[1][0] = 2mat[1][1] = 3mat[2][0] = 4mat[2][1] = 5-------------arr[0][0] = 0arr[0][1] = 1arr[1][0] = 2arr[1][1] = 3arr[2][0] = 4arr[2][1] = 5

Page 9: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

9/12

Strings of Characters

● A string is an array of characters.● A string is always terminated by a null character (the ASCII code 0).

● Characters are selected by integer indexes.● Indexes always start at 0.

Page 10: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

10/12

Declaring Strings

s1s1 and s2s2 are identical.s3s3 and s4s4 are identical.

Do not confuse ‘0’ ‘0’ (ASCII code: 48) and 00 (the null character ; ASCII Code: 0).

See also: table of escape sequences.

s1 = Hello!s2 = Hello!s3 = 3210s4 = 3210

Page 11: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

11/12

Manipulating Strings – Example

l1 = 5l2 = 6l3 = 3buffer = Hellobuffer = World!buffer = Bye

Page 12: The C Language : Arrays and Stringsdebug-pro.com/epita/prog/s3/lecture/arrays_and_strings.pdf · 2020. 9. 15. · 2/12 Arrays An array is a collection of values that have the same

12/12

Command-Line Arguments

$ ./a.out Number of arguments ......... 1argv[0] (program name) ...... "./a.out"

args.cargs.c

$ ./a.out a bc "d e" " fg " hNumber of arguments ......... 6argv[0] (program name) ...... "./a.out"argv[1] ..................... "a"argv[2] ..................... "bc"argv[3] ..................... "d e"argv[4] ..................... " fg "argv[5] ..................... "h"

$ gcc -Wall -Wextra args.c