Docsity

Arrays, Strings, and Pointers, Schemes and Mind Maps of Computer science

Partial preview of the text

Download Arrays, Strings, and Pointers and more Schemes and Mind Maps Computer science in PDF only on Docsity!

Arrays, Strings, and Pointers

Jan Faigl

Department of Computer Science

Faculty of Electrical Engineering

Czech Technical University in Prague

Lecture 04

BE5B99CPL – C Programming Language

Overview of the Lecture

 Part 1 – Arrays

Arrays

Variable-Length Array

Multidimensional Arrays

Initialization

Arrays and Pointers K. N. King: chapters 8 and 12

 Part 2 – Strings

String Literals

String Variable

Reading Strings

C String Library K. N. King: chapters 13

 Part 3 – Pointers

Pointers

const Specifier

Pointers to Functions

Dynamic Allocation K. N. King: chapters 11, 12, 17

File name: lec04-array.tex

Outline

Arrays

Variable-Length Array

Multidimensional Arrays

Initialization

Arrays and Pointers

Arrays – Example 1/

 Example of the array declaration

1 #include <stdio.h> 2 3 int main(void) 4 { 5 int array[10]; 6 7 for (int i = 0; i < 10; i++) { 8 array[i] = i; 9 } 10 11 int n = 5; 12 int array2[n * 2]; 13 14 for (int i = 0; i < 10; i++) { 15 array2[i] = 3 * i - 2 * i * i; 16 } 17 18 printf("Size of array: %lu\n", sizeof(array)); 19 for (int i = 0; i < 10; ++i) { 20 printf("array[%i]=%+2i \t array2[%i]=%6i\n", i, array[i], i, array2[i]); 21 } 22 return 0; 23 }

Size of array: 40 array[0]=+0 array2[0]= 0 array[1]=+1 array2[1]= 1 array[2]=+2 array2[2]= - array[3]=+3 array2[3]= - array[4]=+4 array2[4]= - array[5]=+5 array2[5]= - array[6]=+6 array2[6]= - array[7]=+7 array2[7]= - array[8]=+8 array2[8]= - array[9]=+9 array2[9]= -

lec04/demo-array.c

Arrays – Example 2/

 Example of the array declaration with an initialization

1 #include <stdio.h> 2 3 int main(void) 4 { 5 int array[5] = {0, 1, 2, 3, 4}; 6 7 printf("Size of array: %lu\n", sizeof(array)); 8 for (int i = 0; i < 5; ++i) { 9 printf("Item[%i] = %i\n", i, array[i]); 10 } 11 return 0; 12 }

Size of array: 20 Item[0] = 0 Item[1] = 1 Item[2] = 2 Item[3] = 3 Item[4] = 4

lec04/array-init.c

 Array initialization

double d[] = {0.1, 0.4, 0.5}; // initialization of the array

char str[] = "hallo"; // initialization with the text literal

char s[] = {’h’, ’a’, ’l’, ’l’, ’o’, ’\0’}; //elements

int m[3][3] = { { 1, 2, 3 }, { 4 , 5 ,6 }, { 7, 8, 9 }}; // 2D array

char cmd[][10] = { "start", "stop", "pause" };

Outline

Arrays

Variable-Length Array

Multidimensional Arrays

Initialization

Arrays and Pointers

Variable-Length Array

 C99 allows to determine the size of the array during the program

runtime

Previous versions of C requires compile-time size of the array.

 Array size can be a function argument

void fce(int n) { // int local_array[n] = { 1, 2 }; initialization is not allowed int local_array[n]; // variable length array

printf("sizeof(local_array) = %lu\n", sizeof(local_array)); printf("length of array = %lu\n", sizeof(local_array) / sizeof(int)); for (int i = 0; i < n; ++i) { local_array[i] = i * i; } } int main(int argc, char *argv[]) { fce(argc); return 0; } lec04/fce_var_array.c

 Variable-length array cannot be initialized in the declaration

Outline

Arrays

Variable-Length Array

Multidimensional Arrays

Initialization

Arrays and Pointers

Multidimensional Arrays

 Array can be declared as multidimensional, e.g., two-dimensional

array for storing a matrix

int m[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

printf("Size of m: %lu == %lu\n", sizeof(m), 33sizeof(int));

for (int r = 0; r < 3; ++r) { for (int c = 0; c < 3; ++c) { printf("%3i", m[r][c]); } printf("\n"); }

Size of m: 36 == 36 1 2 3 4 5 6 7 8 9

lec04/matrix.c

Multidimensional Array and Memory Representation

 Multidimensional array is always a continuous block of memory

E.g., int a[3][3]; represents allocated memory of the size

9*sizeof(int), i.e., usually 36 bytes.

int m[3][3] = { { 1, 2, 3 }, { 4, 5, 6}, { 7, 8, 9 } };

int *pm = (int *)m; // pointer to an allocated continuous memory block printf("m[0][0]=%i m[1][0]=%i\n", m[0][0], m[1][0]); // 1 4 printf("pm[0]=%i pm[3]=%i\n", m[0][0], m[1][0]); // 1 4

lec04/matrix.c

1 2 3 4 5 6 7 8 9

Row 0 Row 1 Row 2

 Two-dimensional array can be declared as point to a pointer, e.g.,

 (^) int **a; – pointer to pointer of the int value(s)  (^) A pointer does not necessarily refer to a continuous memory E.g., in a case of pointer to array of pointers  (^) Therefore, when accessing to a as to one-dimensional array int *b = (int *)a; the access to the second (and further) row cannot be guaranteed as in the above example

Outline

Arrays

Variable-Length Array

Multidimensional Arrays

Initialization

Arrays and Pointers

Initialization of Multidimensional Array

 Multidimensional array can be also initialized during the declaration

Two-dimensional array is initialized row by row.

 Using designated initializers, the other elements are set to 0

void print(int m[3][3]) { for (int r = 0; r < 3; ++r) { for (int c = 0; c < 3; ++c) { printf("%4i", m[r][c]); } printf("\n"); } }

int m0[3][3]; int m1[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int m2[3][3] = { 1, 2, 3 }; int m3[3][3] = { [0][0] = 1, [1][1] = 2, [2][2] = 3 };

print(m0); print(m1); print(m2); print(m3);

m0 - not initialized

  • 0 1 0 740314624 0 0

m1 - init by rows 1 2 3 4 5 6 7 8 9

m2 - partial init 1 2 3 0 0 0 0 0 0

m3 - indexed init 1 0 0 0 2 0 0 0 3

lec04/array_inits.c

Outline

Arrays

Variable-Length Array

Multidimensional Arrays

Initialization

Arrays and Pointers