inform ática i

8
Informática I Código 2547100 Semestre 2013-1 Para Ingeniería Electrónica e Ingeniería de Telecomunicaciones Profesor: Sebastián Isaza

Upload: lael-malone

Post on 05-Jan-2016

28 views

Category:

Documents


0 download

DESCRIPTION

Inform ática I. Código 2547100 Semestre 2013-1 Para Ingeniería Electrónica e Ingeniería de Telecomunicaciones Profesor: Sebastián Isaza. Arrays and pointers. El nombre de un arreglo es siempre la dirección del primer elemento de ese arreglo - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Inform ática I

Informática I

Código 2547100Semestre 2013-1

Para Ingeniería Electrónicae Ingeniería de Telecomunicaciones

Profesor: Sebastián Isaza

Page 2: Inform ática I

Informática I (2013-1) – Prof. Sebastián Isaza 2

•El nombre de un arreglo es siempre la dirección del primer elemento de ese arreglo•Si lista es un arreglo, entonces lo siguiente es verdadero

lista == &lista[0]

•Tanto lista como &lista[0] representan la dirección del primer elemento del arreglo lista ¡y son constantes!

Arrays and pointers

Page 3: Inform ática I

Informática I (2013-1) – Prof. Sebastián Isaza 3

int *big = 0x2348;big++;short *small = 0x2348;small++;small++;

Stepping with pointers0x2348

0x2349

0x234A

0x234B

0x234C

0x234D

0x234E

0x234F

0x2350

0x2351

0x2352

0x2353

0x2354

0x2355

0x2356

0x2357

big

small

0x2348

0x2348

0x234C

0x234A0x234C

Page 4: Inform ática I

Informática I (2013-1) – Prof. Sebastián Isaza 4

short sm[5]short *ptr;ptr = sm;

Arrays and pointers

0x2348

0x2349

0x234A

0x234B

0x234C

0x234D

0x234E

0x234F

0x2350

0x2351

0x2352

0x2353

sm[0]

sm[1]

sm[2]

sm[3]

sm[4]

sm[5]

ptr

ptr+1

ptr+2

ptr+3

ptr+4

ptr+5

Page 5: Inform ática I

Informática I (2013-1) – Prof. Sebastián Isaza 5

int sum(int *ar, int n);int sum(int *, int n);int sum(int ar[], int n);int sum(int [], int n);

int sum(int *ar, int n){// code

}

int sum(int ar[], int n){// code

}

Arrays as function parameters

Page 6: Inform ática I

Informática I (2013-1) – Prof. Sebastián Isaza 6

int sum (const int ar[], int n);

int main(void){ int res; int values[4] = {1, 6, 8, 4}; res = sum (values, 4);}

int sum (const int ar[], int n){ int i, total; for (total = 0, i = 0; i < n; i++){ total += ar[i]; } return total;}

Protecting array contentsprototipo de la función

llamado de sum() en main()

declaración de variables en main()

definición de la función

Page 7: Inform ática I

Informática I (2013-1) – Prof. Sebastián Isaza 7

•Asignación: ptr = &var

•Derreferenciación: *ptr

•Obtener dirección de un apuntador: &ptr

•Suma/Resta de un entero a un apuntador: ptr+1

•Incremento/Decremento de un apuntador: ptr++

•Resta de apuntadores: ptr1-ptr2

•Comparación de apuntadores: ptr1<ptr2

Pointer operations

Page 8: Inform ática I

Informática I (2013-1) – Prof. Sebastián Isaza 8

int A[3][2]={{12,18},{20,26},{28,34}};A == ?A[0] == ?A + 1 == ?A[0] + 1 == ?*(A[0]) == ?*A == ?**A == *(*A) ==?

Double indirection

… …

0xFC58 12

0xFC5C 18

0xFC60 20

0xFC64 26

0xFC68 28

0xFC6C 34

0xFC70 …

A[2][0]

A[0]

A[1]

A[2]

A[0][0]

A[0][1]

A[1][0]

A[1][1]

A[2][1]

12 18

20 26

28 34