CSC 2400: Computer Systems Arrays and Strings in C - Villanova

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¡¯ i.e. NULL

! Functions for manipulating strings

Arrays: C vs. Java

Java

C

Arrays

int [] a = new int [5];

int [] a = {7,2,5,4,11};

float [][] b =

new float [5][20];

int a[5];

int a[] = {7,2,5,4,11};

float b[5][20];

Array bound

checking

// run-time check

/* no run-time check */

Arrays in C

Label Address

int a[5];

a

217

226

a[0]

a[1]

a[2]

a[3]

a[4]

400

404

408

412

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)

What would printf(¡±%d¡±, a); print out?

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]!

a

100

104

108

112

116

120

Memory Address

a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

Label

? Note that a[1] is ALWAYS at higher address than a[0], and so on

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download