12
Universidade Federal da Paraíba Centro de Informática The C Programming Language: Part II Lecture 3 1107186 – Estruturas de Dados Prof. Christian Azambuja Pagot CI / UFPB

Data Structures - 03. C Review, II

Embed Size (px)

DESCRIPTION

Slide da cadeira de Estrutura de Dados, ministrado pelo Prof. Dr. Christian Pagot, na Universidade Federal da Paraíba.

Citation preview

Page 1: Data Structures - 03. C Review, II

Universidade Federal da ParaíbaCentro de Informática

The C Programming Language:Part II

Lecture 3

1107186 – Estruturas de Dados

Prof. Christian Azambuja PagotCI / UFPB

Page 2: Data Structures - 03. C Review, II

2Universidade Federal da ParaíbaCentro de Informática

Strings in C

● C does not have a type string. ● Strings are implemented as arrays of

characters terminated with binary zero (\0).● Example:

char *name;

C code excerpt:

Page 3: Data Structures - 03. C Review, II

3Universidade Federal da ParaíbaCentro de Informática

1

Strings in C (cont.)

● How to fill a string?

char *name;...name[0] = 'C';name[1] = 'h';name[2] = 'r';name[3] = 'i';name[4] = 's';name[5] = '\0';

C code excerpt:

char name[6] = {'C','h','r','i','s','\0'};

C code excerpt:

char name[6] = “Chris”;

C code excerpt:

char *name;...*(name)   = 'C';*(name+1) = 'h';*(name+2) = 'r';*(name+3) = 'i';*(name+4) = 's';*(name+5) = '\0';

C code excerpt:2

3

4

Page 4: Data Structures - 03. C Review, II

4Universidade Federal da ParaíbaCentro de Informática

Strings in C (cont.)

● How to copy a string?

char *name1 = “Chris”;char *name2;...name2 = name1;

C code excerpt:

It just copies the address of the first char of the string pointed

by name1 to the pointer name2!

Page 5: Data Structures - 03. C Review, II

5Universidade Federal da ParaíbaCentro de Informática

Strings in C (cont.)

● How to copy a string?

void my_strcpy(char *destination,    char *source)

{char *p = destination;

while (*source != '\0'){

*p++ = *source++;}

*p = '\0';}

C code excerpt:

char *name1 = “Chris”;char *name2;...my_strcpy(name2,name1);

C code excerpt:

Adapted from “A Tutorial on Pointers and Arrays in C”, by Ted Jensen.

Page 6: Data Structures - 03. C Review, II

6Universidade Federal da ParaíbaCentro de Informática

Suggested Exercises

● Read about the following C string functions, and, through inverse engineering, implement your own versions of those functions:– strlen();

– strcat();

– strchr();

Adapted from “A Tutorial on Pointers and Arrays in C”, by Ted Jensen.

Page 7: Data Structures - 03. C Review, II

7Universidade Federal da ParaíbaCentro de Informática

2D Arrays in C

● Creating 2D arrays with array notation:

● Writing to array elements:

char x[2][3];

C code excerpt:

x[0][0] = 'a';x[0][1] = 'b';x[0][2] = 'c';x[1][0] = 'd';x[1][1] = 'e';x[1][2] = 'f';

C code excerpt:

0 1 2

0 a b c

1 d e fLine

Column

Page 8: Data Structures - 03. C Review, II

8Universidade Federal da ParaíbaCentro de Informática

2D Arrays in C

● Writing to array elements (cont.):

char x[2][3];...x[0] = {'a','b','c'};x[1] = {'d','e','f'};

C code excerpt:

0 1 2

0 a b c

1 d e fLine

Column

char x[2][3];...x[0] = “abc”;x[1] = “def”;

C code excerpt:

C strings imply an extra char for the “\0”!

In the above case, x should be declared as:

char x[2][4];

Page 9: Data Structures - 03. C Review, II

9Universidade Federal da ParaíbaCentro de Informática

2D Arrays in C

● Actual array storage pattern in memory:

char x[2][3];...x[0] = {'a','b','c'};x[1] = {'d','e','f'};

C code excerpt:

0 1 20 a b c

1 d e f

Logical arrayarrangement.

&x[0][0]

1 byte

All n-dimensional arrays are linearized in

physical memory!

Addr.

Value Physical memory.

i i+1 i+2 i+3 i+4 i+5a b c d e f

. . . . . .

Page 10: Data Structures - 03. C Review, II

10Universidade Federal da ParaíbaCentro de Informática

Simulating 2D Array with Pointers

char x1[2][3];

C code excerpt (array):

char (*x2)[3];

C code excerpt (pointer-based simulation):

int row, col;…*(*(x2 + row) + col) = 'a';

1 byteoffset.

3 bytesoffset.

Addr.

Value Physical memory.

i i+1 i+2 i+3 i+4 i+5a b c d e f

. . . . . .

x1[0][0]

x1[0][1]

x1[1][0]x2 is a pointer to an array (with 3 elements) of char.

C code example...(ptr2.c)

Page 11: Data Structures - 03. C Review, II

11Universidade Federal da ParaíbaCentro de Informática

Pointers and Structures

● Suppose the following structure:

struct person {char name[20];int age;float weight;

};

C code excerpt:

struct person a;

...

printf(“%s”, a.name);

C code excerpt:

struct person *b;

...

printf(“%s”, (*b).name);

printf(“%s”, b­>name);

The sameas '(*b).'

How to print the name field?

How to print the name field?

Page 12: Data Structures - 03. C Review, II

12Universidade Federal da ParaíbaCentro de Informática

Function Parameters

● Parameters are passed by value in C.

void function1(int a){

a = a + 1;};

C code excerpt:

...

int age = 15;

function1(age);

printf(“%i”, age);

void function1(int *a){

*a = *a + 1;};

C code excerpt:

...

int age = 15;

function1(&age);

printf(“%i”, age);

15 16

Parameter a is still a copy!!!