Structures, Strings, and Such What about arrays? Arrays are - - PDF document

structures strings and such what about arrays
SMART_READER_LITE
LIVE PREVIEW

Structures, Strings, and Such What about arrays? Arrays are - - PDF document

Structures, Strings, and Such What about arrays? Arrays are declared: 0 arr[0] int arr[8]; 0 arr[1] This declares an array of 8 0 arr[2] integers 0 arr[3] Memory allocated for it, but 5 0 arr[4] not initialized 0 arr[5]


slide-1
SLIDE 1

Structures, Strings, and Such

Structures, Strings, and Such

2 CMPS 12B, UC Santa Cruz

What about arrays?

Arrays are declared:

int arr[8];

This declares an array of 8

integers

Memory allocated for it, but

not initialized

Arrays are referenced:

arr[4] = 5; z = arr[4];

No bounds checking: arr[51]

won’t signal an error

Not easy to find the length

  • f an array

z arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] 5 5

slide-2
SLIDE 2

Structures, Strings, and Such

3 CMPS 12B, UC Santa Cruz

Arrays and pointers

Arrays are implemented

with pointers

&(arr[0]) == arr == address

  • f the first element of the

array

arr[j] == *(arr + j)

Any integer can be added to

a pointer

Negative ones, too! As before, C doesn’t check to

see if it looks OK z arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7]

Structures, Strings, and Such

4 CMPS 12B, UC Santa Cruz

Accessing arrays

Common to access arrays in

loops

Two forms of loops are

common

Array index changes Pointer to element changes

(incremented)

Both forms are OK (and

equivalent)

int arr[50]; int j; int *p; int sum = 0; for (j = 0; j < 50; j++) { sum += arr[j]; } for (p = arr; p < (arr+50); p++) { sum += *p; }

slide-3
SLIDE 3

Structures, Strings, and Such

5 CMPS 12B, UC Santa Cruz

Strings in C

Strings in C are simply arrays of characters

Strings must be terminated by NUL (\0) This makes it impossible to include \0 in a string…

Since arrays can’t be resized, nor can strings!

They can be copied onto one another using functions like strcpy()

Strings can be declared like this:

char s1[50]; char s2[50] = “Hello!”;

Now, s2 is an array of 50 bytes, with the first 7 initialized

char *s3 = “testing”;

Now, s3 points to an array of at least 8 characters (including the trailing

\0)

Some smart compilers may share storage for multiple strings

initialized to “test”…

Don’t modify strings initialized the third way!

Structures, Strings, and Such

6 CMPS 12B, UC Santa Cruz

Structures in C

  • A s

t r uct ur e is a mechanism for grouping values together

Similar to class in Java No associated methods! May contain builtin types as well

as pointers and other structures

  • Refer to elements of a structure

with “.” notation

Different notation for referring

to elements of a structure being pointed to

  • Useful for building complex data

structures

Pointers may refer to memory

used for a structure

How is memory allocated?

struc t tre e node { int strLe ngth; c har s[8]; struc t tre e node *l; struc t tre e node *r; }; struc t tre e node root; root.strLe ngth = 5; // Copy 5 c harac te rs into s bc opy (value , root.s, 5);

slide-4
SLIDE 4

Structures, Strings, and Such

7 CMPS 12B, UC Santa Cruz

Pointers and structures

Pointers may point at

structures

p = &root;

Elements referred to with

“->” notation

(*p).strLength is equivalent

to p->strLength

How is space allocated?

Library call mal

l

  • c

Compute necessary memory

size using s i zeof function

Works on builtin types, too May modify the value

struc t tre e node { int strLe ngth; c har s[8]; struc t tre e node *l; struc t tre e node *r; }; struc t tre e node root; struc t tre e node *p; root.strLe ngth = 5; // Copy 5 c harac te rs into s bc opy (value , root.s, 5); p->strLe ngth = 6; bc opy (nValue , p->s, 6); node p = (struc t tre e node *) malloc (siz e of (struc t tre e node ); root.l = node p;

Structures, Strings, and Such

8 CMPS 12B, UC Santa Cruz

Passing parameters

Builtin types (int, pointers,

FP) are passed by v al ue

Function cannot change the

value in the caller

Functions may change the

value a pointer points at

Arrays and structures must

be passed by r ef er ence

Pass a pointer to the array or

structure

Pointer doesn’t change Array or structure may

change

void addOne Wrong (int x) { x = x+1; } void addOne Right (int *x) { *x = *x +1; } int main () { int value = 5; addOne Wrong (value ); printf (“Value is %d\n”, value ); addOne Right (&value ); printf (“Value is %d\n”, value ); re turn (0); } Value is 5 Value is 6