Top Banner
Programação Estruturada Prof. Rodrigo Hausen http://progest.compscinet.org ┌─────────────────────────┐ │ Ponteiros e Passagem de │▒ Parâmetros │▒ └─────────────────────────┘▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 1
141

Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *p

Aug 06, 2018

Download

Documents

truongmien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Programação Estruturada Prof. Rodrigo Hausen http://progest.compscinet.org

┌─────────────────────────┐ │ Ponteiros e Passagem de │▒ │ Parâmetros │▒ └─────────────────────────┘▒ ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

1

Page 2: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

AULA PASSADA - ponteiros

Uma declaração tal como:

tipo *nome;

Declara um ponteiro: referência a uma posição de memória onde se encontra uma variável daquele tipo.

Exemplos:

int *numeros;

double *notas;

char *nomeDoMes;

Obs.: se apenas declararmos o ponteiro, ele indicará uma região indeterminada. 2

Page 3: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

AULA PASSADA - ponteiros

Podemos declarar um ponteiro, alocar uma região de memória no heap e fazer a referência apontar para essa região alocada:

tipo *ptr = (tipo *)malloc(n*sizeof(tipo));

O que isto faz:1) declara ponteiro ptr para tipo2) aloca espaço para n elementos, cada um com tamanho sizeof(tipo)3) (tipo *) converte o ponteiro sem tipo retornado por malloc p/ tipo correto 4) faz ptr apontar para o primeiro elemento da região

3

Page 4: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS – exemplo exptr.c

#include <stdlib.h> // para usar malloc#include <stdio.h> // para usar puts

int main(void) { char *p = (char*)malloc(3*sizeof(char)); if (p == NULL) return 1; p[0] = 'o'; p[1] = 'i'; p[2] = '\0'; puts(p); p[0] = 'h'; puts(p); free(p);}

4

Page 5: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS – exemplo exptr.c

#include <stdlib.h> // para usar malloc#include <stdio.h> // para usar puts

int main(void) { char *p = (char*)malloc(3*sizeof(char)); if (p == NULL) return 1; p[0] = 'o'; p[1] = 'i'; p[2] = '\0'; puts(p); p[0] = 'h'; puts(p); free(p);}

5

Sempre verifique se o ponteiro retornado por

malloc é válido!

Page 6: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS – exemplo exptr.c

#include <stdlib.h> // para usar malloc#include <stdio.h> // para usar puts

int main(void) { char *p = (char*)malloc(3*sizeof(char)); if (p == NULL) return 1; p[0] = 'o'; p[1] = 'i'; p[2] = '\0'; puts(p); p[0] = 'h'; puts(p); free(p);}

6

Após o último uso, sempre libere a área

alocada.

Page 7: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS – exemplo exptr.c

#include <stdlib.h> // para usar malloc#include <stdio.h> // para usar puts

int main(void) { char *p = (char*)malloc(3*sizeof(char)); if (p == NULL) return 1; p[0] = 'o'; p[1] = 'i'; p[2] = '\0'; puts(p); p[0] = 'h'; puts(p); free(p)}

7

Saída:

oihi

Page 8: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS – exemplo exptr2.c

Edite e compile o programa exptr2.c:

#include <stdio.h> // para usar puts

int main(void) { char str[] = "oi"; char *p = str; puts(str); puts(p); p[0] = 'h'; puts(str); puts(p);}

Explique o comportamento do programa. 8

Page 9: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS – exemplo exptr2.c

Edite e compile o programa exptr2.c:

#include <stdio.h> // para usar puts

int main(void) { char str[] = "oi"; char *p = str; puts(str); puts(p); p[0] = 'h'; puts(str); puts(p);}

Explique o comportamento do programa. 9

Saída:oioihihi

Page 10: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS – exemplo exptr2.c

Edite e compile o programa exptr2.c:

#include <stdio.h> // para usar puts

int main(void) { char str[] = "oi"; char *p = str; puts(str); puts(p); p[0] = 'h'; puts(str); puts(p);}

p e str apontam para a mesma região de mem.10

Saída:oioihihi

Page 11: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS – exemplo exptr3.c

Edite e compile o programa exptr3.c:

#include <stdio.h> // para usar puts

int main(void) { char *str = "oi"; char *p = str; puts(str); puts(p); p[0] = 'h'; puts(str); puts(p);}

O que houve? 11

Page 12: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr3.c

Edite e compile o programa exptr3.c:

#include <stdio.h> // para usar puts

int main(void) { char *str = "oi"; char *p = str; puts(str); puts(p); p[0] = 'h'; puts(str); puts(p);}

O que houve? 12

Saída:oioiFalha de segmentação

Page 13: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr3.c

int main(void) {

char *str = "oi";

char *p = str;

puts(str);

puts(p);

p[0] = 'h';

puts(str);

puts(p);

}13

faz str apontar para a constante “oi”

Page 14: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr3.c

int main(void) {

char *str = "oi";

char *p = str;

puts(str);

puts(p);

p[0] = 'h';

puts(str);

puts(p);

}14

faz p apontar para a mesma região de memória que str

(lembre: a região de memória é imutável pois guarda uma

constante)

Page 15: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr3.c

int main(void) {

char *str = "oi";

char *p = str;

puts(str);

puts(p);

p[0] = 'h';

puts(str);

puts(p);

}15

imprime a string apontada por str

Page 16: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr3.c

int main(void) {

char *str = "oi";

char *p = str;

puts(str);

puts(p);

p[0] = 'h';

puts(str);

puts(p);

}16

imprime a string apontada por p

Page 17: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr3.c

int main(void) {

char *str = "oi";

char *p = str;

puts(str);

puts(p);

p[0] = 'h';

puts(str);

puts(p);

}17

tenta alterar o primeiro caractere da região apontada por p

como a região apontada por p faz parte de área de memória imutável...

Page 18: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Moral dos exemplos exptr1.c a exptr3.c

• Um ponteiro pode indicar uma área de memória no heap, na pilha ou em outras regiões de memória (se houver), que podem ser imutáveis

• Declara ponteiro e aloca espaço no heap (deve ser desalocado c/ free): char *p = (char *)malloc(tamanho);

• Declara ponteiro e aloca espaço na pilha (desalocação automática): char p[] = "inicialização";

• Declara ponteiro e aponta p/ uma constante (região imutável): char *p = "inicialização"; 18

Page 19: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr4.c

#include <stdio.h>

int main(void) { int i = 42; int *p = &i;

printf("i é %d\n", i); p[0]++; printf("i é %d\n", i);}

19

Page 20: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr4.c

#include <stdio.h>

int main(void) { int i = 42; int *p = &i;

printf("i é %d\n", i); p[0]++; printf("i é %d\n", i);}

20

Operador &retorna referência (endereço)da posição de memóriaonde está armazenada avariável

Page 21: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr4.c

#include <stdio.h>

int main(void) { int i = 42; int *p = &i;

printf("i é %d\n", i); p[0]++; printf("i é %d\n", i);}

Por que o valor de i mudou? Por que não fez “cabum”?

21

Saída:

i é 42i é 43

Page 22: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr5.c

#include <stdio.h>

int main(void) { int i[] = { 42, 99 }; int *p = i;

printf("i[0] é %d\n", i[0]); p[0]++; printf("i[0] é %d\n", i[0]);}

Funciona?

22

Page 23: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PONTEIROS - exemplo exptr6.c

#include <stdio.h>

int main(void) { static const int i[] = { 42, 99 }; int *p = i;

printf("i[0] é %d\n", i[0]); p[0]++; printf("i[0] é %d\n", i[0]);}

Funciona?

23

Page 24: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

OPERAÇÕES com PONTEIROS

Operador de referência &

Colocado na frente de uma variável, nos dá o endereço onde esta variável está armazenada.

int i = 42;int *p = &i;

p[0]++; // incrementa o primeiro int // na região de memória apontada // por p

24

Page 25: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

OPERAÇÕES com PONTEIROS

Operador de referência &

Colocado na frente de uma variável, nos dá o endereço onde esta variável está armazenada.

Por que não se usa o operador & abaixo?

int i[] = { 42, 99 };int *p = i;

p[0]++;

25

Page 26: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

OPERAÇÕES com PONTEIROS

Operador de dereferência *

Colocado na frente de uma variável ou expressão, permite acessar o conteúdo da posição de memória apontada por essa variável ou expressão.

int i = 42;int *p = &i;

++(*p); // equivale a ++p[0]

26

Atenção! Neste exmplo, apenas este uso do caractere * indica o operador de dereferência!

Page 27: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

OPERAÇÕES com PONTEIROS

Operador de dereferência *

Infelizmente, a notação é confusa. O caractere * indica dereferência se não vier imediatamente precedido por um tipo.

int *p; // declara variável do tipo // ponteiro para int

*p = 42; // coloca na posição de memória // apontada por p o número 42

27

Page 28: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Experimento no cling

Inicie o cling (se já tiver iniciado, saia com .q e reinicie-o)

[cling]$ #include <stdio.h>[cling]$ int a[] = { 18, 33, 99 };[cling]$ int b[] = { 42, 21 };

28

Page 29: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Experimento no cling

Inicie o cling (se já tiver iniciado, saia com .q e reinicie-o)

[cling]$ #include <stdio.h>[cling]$ int a[] = { 18, 33, 99 };[cling]$ int b[] = { 42, 21 };[cling]$ int *p = a;[cling]$ int *q = b;

29

Page 30: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Experimento no cling

Inicie o cling (se já tiver iniciado, saia com .q e reinicie-o)

[cling]$ #include <stdio.h>[cling]$ int a[] = { 18, 33, 99 };[cling]$ int b[] = { 42, 21 };[cling]$ int *p = a;[cling]$ int *q = b;[cling]$ printf("%d\n", p);

30

Page 31: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Experimento no cling

Inicie o cling (se já tiver iniciado, saia com .q e reinicie-o)

[cling]$ #include <stdio.h>[cling]$ int a[] = { 18, 33, 99 };[cling]$ int b[] = { 42, 21 };[cling]$ int *p = a;[cling]$ int *q = b;[cling]$ printf("%d\n", p);Warning: … (tentando imprimir um ponteiro com “%d”)

31

Page 32: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Experimento no cling

Inicie o cling (se já tiver iniciado, saia com .q e reinicie-o)

[cling]$ #include <stdio.h>[cling]$ int a[] = { 18, 33, 99 };[cling]$ int b[] = { 42, 21 };[cling]$ int *p = a;[cling]$ int *q = b;[cling]$ printf("%d\n", p);Warning: … (tentando imprimir um ponteiro com “%d”) [cling]$ printf("%d\n", *p);

32

Page 33: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Experimento no cling

Inicie o cling (se já tiver iniciado, saia com .q e reinicie-o)

[cling]$ #include <stdio.h>[cling]$ int a[] = { 18, 33, 99 };[cling]$ int b[] = { 42, 21 };[cling]$ int *p = a;[cling]$ int *q = b;[cling]$ printf("%d\n", p);Warning: … (tentando imprimir um ponteiro com “%d”) [cling]$ printf("%d\n", *p);18

33

Page 34: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Experimento no cling

Inicie o cling (se já tiver iniciado, saia com .q e reinicie-o)

[cling]$ #include <stdio.h>[cling]$ int a[] = { 18, 33, 99 };[cling]$ int b[] = { 42, 21 };[cling]$ int *p = a;[cling]$ int *q = b;[cling]$ printf("%d\n", p);Warning: … (tentando imprimir um ponteiro com “%d”) [cling]$ printf("%d\n", *p);18[cling]$ printf("%d\n", *q);

34

Page 35: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Experimento no cling

Inicie o cling (se já tiver iniciado, saia com .q e reinicie-o)

[cling]$ #include <stdio.h>[cling]$ int a[] = { 18, 33, 99 };[cling]$ int b[] = { 42, 21 };[cling]$ int *p = a;[cling]$ int *q = b;[cling]$ printf("%d\n", p);Warning: … (tentando imprimir um ponteiro com “%d”) [cling]$ printf("%d\n", *p);18[cling]$ printf("%d\n", *q);42

35

Page 36: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

Experimento no cling

Inicie o cling (se já tiver iniciado, saia com .q e reinicie-o)

[cling]$ #include <stdio.h>[cling]$ int a[] = { 18, 33, 99 };[cling]$ int b[] = { 42, 21 };[cling]$ int *p = a;[cling]$ int *q = b;[cling]$ printf("%d\n", p);Warning: … (tentando imprimir um ponteiro com “%d”) [cling]$ printf("%d\n", *p);18[cling]$ printf("%d\n", *q);42

36

Permaneça no cling!

Já voltaremos a ele...

Page 37: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

Podemos fazer contas aritméticas e apontar para outras regiões de memória.

[cling]$ int a[]={ 18,33,99 }; int*p=a;[cling]$ int b[] = { 42, 21 }; int*q=b;[cling]$ q-p3[cling]$ printf("end. de p: %p\n", p);[cling]$ printf("end. de q: %p\n", q);

37

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲q

Page 38: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

Podemos fazer contas aritméticas e apontar para outras regiões de memória.

[cling]$ int a[]={ 18,33,99 }; int*p=a;[cling]$ int b[] = { 42, 21 }; int*q=b;[cling]$ q-p3[cling]$ printf("end. de p: %p\n", p);[cling]$ printf("end. de q: %p\n", q);

38

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1

▲p+2

▲p+3 == q

▲q + 1

Page 39: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

[cling]$ *p(int) 18[cling]$ *q(int) 42[cling]$ *(p+2)(int) 99[cling]$ *(p+3)(int) 42

39

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1

▲p+2

▲p+3 == q

▲q + 1

Page 40: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

[cling]$ *(++p)▉

40

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1

▲p+2

▲p+3 == q

▲q + 1

Não pressione enter ainda!O que isto faz?

Qual será o resultado?

Page 41: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

[cling]$ *(++p)(int) 33[cling]$ ▉

41

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1

▲p+2 == q

▲q + 1

Page 42: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

[cling]$ *(++p)(int) 33[cling]$ *(p++)▉

42

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1

▲p+2 == q

▲q + 1

Page 43: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

[cling]$ *(++p)(int) 33[cling]$ *(p++)(int) 33[cling]$ ▉

43

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1 == q

▲q + 1

Page 44: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

[cling]$ *(++p)(int) 33[cling]$ *(p++)(int) 33[cling]$ *p▉

44

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1 == q

▲q + 1

Page 45: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

[cling]$ *(++p)(int) 33[cling]$ *(p++)(int) 33[cling]$ *p(int) 99[cling]$ ▉

45

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1 == q

▲q + 1

Page 46: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

[cling]$ *(++p)(int) 33[cling]$ *(p++)(int) 33[cling]$ *p(int) 99[cling]$ *(--p)▉

46

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1 == q

▲q + 1

Page 47: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS

[cling]$ *(++p)(int) 33[cling]$ *(p++)(int) 33[cling]$ *p(int) 99[cling]$ *(--p)(int) 33[cling]$ ▉

47

─┬──────┬──────┬──────┬──────┬──────┬─ │ 18 │ 33 │ 99 │ 42 │ 21 │─┴──────┴──────┴──────┴──────┴──────┴─

▲p

▲p+1

▲p+2 == q

▲q + 1

Page 48: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

int palindromo(const char *p) {

}

48

Page 49: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

int palindromo(const char *p) { const char *q = p;

}

49

Page 50: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

int palindromo(const char *p) { const char *q = p; // localiza final da string while (*q != '\0') ++q;

}

50

Page 51: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

int palindromo(const char *p) { const char *q = p; // localiza final da string while (*q != '\0') ++q; --q; // q aponta para último char // diferente de '\0'

}

51

Page 52: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

int palindromo(const char *p) { const char *q = p; // localiza final da string while (*q != '\0') ++q; --q; // q aponta para último char // diferente de '\0' // compara while (p < q) {

}

}

52

Page 53: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

int palindromo(const char *p) { const char *q = p; // localiza final da string while (*q != '\0') ++q; --q; // q aponta para último char // diferente de '\0' // compara while (p < q) { if (*p != *q) return 0; // falso

}

}

53

Page 54: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

int palindromo(const char *p) { const char *q = p; // localiza final da string while (*q != '\0') ++q; --q; // q aponta para último char // diferente de '\0' // compara while (p < q) { if (*p != *q) return 0; // falso ++p; --q; }

}

54

Page 55: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

int palindromo(const char *p) { const char *q = p; // localiza final da string while (*q != '\0') ++q; --q; // q aponta para último char // diferente de '\0' // compara while (p < q) { if (*p != *q) return 0; // falso ++p; --q; } return 1; // verdadeiro}

55

Page 56: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

Reinicie o cling e teste:

[cling]$ .L palindromo.c[cling]$ palindromo("roma amor")▉

56

Page 57: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

Reinicie o cling e teste:

[cling]$ .L palindromo.c[cling]$ palindromo("roma amor")(int) 1 // verdadeiro[cling]$ ▉

57

Page 58: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

Reinicie o cling e teste:

[cling]$ .L palindromo.c[cling]$ palindromo("roma amor")(int) 1 // verdadeiro[cling]$ palindromo("anemona")▉

58

Page 59: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

Reinicie o cling e teste:

[cling]$ .L palindromo.c[cling]$ palindromo("roma amor")(int) 1 // verdadeiro[cling]$ palindromo("anemona")(int) 0 // falso[cling]$ ▉

59

Page 60: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

Reinicie o cling e teste:

[cling]$ .L palindromo.c[cling]$ palindromo("roma amor")(int) 1 // verdadeiro[cling]$ palindromo("anemona")(int) 0 // falso[cling]$ palindromo("")▉

60

Page 61: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ARITMÉTICA DE PONTEIROS – palindromo.c

Reinicie o cling e teste:

[cling]$ .L palindromo.c[cling]$ palindromo("roma amor")(int) 1 // verdadeiro[cling]$ palindromo("anemona")(int) 0 // falso[cling]$ palindromo("")(int) 1 // verdadeiro[cling]$ ▉

A string vazia é palíndroma, pois equivale a ela mesma lida de trás para frente. 61

Page 62: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

▶ int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

62

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─▲p

'\0'

Page 63: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { ▶ const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

63

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p▲q

Page 64: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

64

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p▲q

Page 65: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

65

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 66: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

66

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 67: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

67

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 68: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

68

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 69: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

69

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 70: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

70

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 71: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

71

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 72: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

72

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 73: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

73

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 74: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; ▶ --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

74

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 75: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

▶ while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

75

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 76: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { ▶ if (*p != *q) return 0; ++p; --q; } return 1; }

76

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 77: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

77

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 78: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

78

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 79: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

▶ while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

79

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 80: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { ▶ if (*p != *q) return 0; ++p; --q; } return 1; }

80

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 81: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

81

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 82: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

82

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 83: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

▶ while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

83

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 84: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { ▶ if (*p != *q) return 0; ++p; --q; } return 1; }

84

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 85: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

85

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 86: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

86

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 87: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

▶ while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

87

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 88: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { ▶ if (*p != *q) return 0; ++p; --q; } return 1; }

88

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 89: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

89

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 90: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

90

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 91: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

91

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p▲q

Page 92: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

▶ while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

92

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p▲q

Page 93: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } ▶ return 1; }

93

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'r'│'o'│'m'│'a'│' '│'a'│'m'│'o'│'r'│ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p▲q

Page 94: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

▶ int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

94

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

Page 95: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { ▶ const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

95

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p▲q

Page 96: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

96

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p▲q

Page 97: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

97

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 98: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

98

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 99: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

99

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 100: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

100

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 101: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

101

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 102: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

102

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 103: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

103

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 104: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; ▶ --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

104

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 105: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

▶ while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

105

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 106: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { ▶ if (*p != *q) return 0; ++p; --q; } return 1; }

106

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 107: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { ▶ if (*p != *q) return 0; ++p; --q; } return 1; }

107

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 108: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

108

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 109: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

109

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 110: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

▶ while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

110

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 111: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { ▶ if (*p != *q) return 0; ++p; --q; } return 1; }

111

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 112: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

112

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 113: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ▶ ++p; --q; } return 1; }

113

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 114: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

▶ while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

114

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 115: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { ▶ if (*p != *q) return 0; ++p; --q; } return 1; }

115

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 116: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { ▶ if (*p != *q) return 0; ++p; --q; } return 1; }

116

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

verdadeiro,pois 'e' ≠ 'o'

Page 117: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q)▶return 0; ++p; --q; } return 1; }

117

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │'a'│'n'│'e'│'m'│'o'│'n'│'a'│ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 118: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

▶ int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

118

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │ │ │ │ │ │ │ │ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

Page 119: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { ▶ const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

119

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │ │ │ │ │ │ │ │ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p▲q

Page 120: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

▶ while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

120

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │ │ │ │ │ │ │ │ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p▲q

Page 121: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; ▶ --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

121

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │ │ │ │ │ │ │ │ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 122: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

▶ while (p < q) { if (*p != *q) return 0; ++p; --q; } return 1; }

122

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │ │ │ │ │ │ │ │ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 123: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

int palindromo(const char *p) { const char *q = p;

while (*q != '\0') ++q; --q;

while (p < q) { if (*p != *q) return 0; ++p; --q; } ▶ return 1; }

123

─┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬─ │ │ │ │ │ │ │ │ │ │ │─┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴─

'\0'

▲p

▲q

Page 124: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Crie o arquivo incrementa.c

void incrementa(int i) { ++i;}

Teste no cling:

[cling]$ .L incrementa.c▉

124

Page 125: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Crie o arquivo incrementa.c

void incrementa(int i) { ++i;}

Teste no cling:

[cling]$ .L incrementa.c[cling]$ int i = 0;▉

125

Page 126: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Crie o arquivo incrementa.c

void incrementa(int i) { ++i;}

Teste no cling:

[cling]$ .L incrementa.c[cling]$ int i = 0;[cling]$ incrementa(i)▉

126

Page 127: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Crie o arquivo incrementa.c

void incrementa(int i) { ++i;}

Teste no cling:

[cling]$ .L incrementa.c[cling]$ int i = 0;[cling]$ incrementa(i)[cling]$ ▉

127

Page 128: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Crie o arquivo incrementa.c

void incrementa(int i) { ++i;}

Teste no cling:

[cling]$ .L incrementa.c[cling]$ int i = 0;[cling]$ incrementa(i)[cling]$ i▉

128

Page 129: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Crie o arquivo incrementa.c

void incrementa(int i) { ++i;}

Teste no cling:

[cling]$ .L incrementa.c[cling]$ int i = 0;[cling]$ incrementa(i)[cling]$ i(int) 0[cling]$ ▉

129

Page 130: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Crie o arquivo incrementa.c

void incrementa(int i) { ++i;}

Teste no cling:

[cling]$ .L incrementa.c[cling]$ int i = 0;[cling]$ incrementa(i)[cling]$ i(int) 0[cling]$ ▉

130

POR QUE VOCÊ FAZ ISSO COMIGO, C !?!?!!!!!11!!UM!!

Page 131: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

void incrementa(int i) { ++i;}

• Em C, a passagem de parâmetros para uma função é sempre por valor, ou seja, quando passamos uma variável para uma função, o valor dessa variável é copiado para o parâmetro correspondente.

• Se quisermos alterar uma variável passada como parâmeto, temos que passar uma referência a ela.

131

Page 132: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Corrija o arquivo incrementa.c

void incrementa(int *i) { ++(*i);}

Reinicie o cling e teste:

[cling]$ .L incrementa.c▉

132

Page 133: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Corrija o arquivo incrementa.c

void incrementa(int *i) { ++(*i);}

Reinicie o cling e teste:

[cling]$ .L incrementa.c[cling]$ int i = 0;▉

133

Page 134: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Corrija o arquivo incrementa.c

void incrementa(int *i) { ++(*i);}

Reinicie o cling e teste:

[cling]$ .L incrementa.c[cling]$ int i = 0;[cling]$ incrementa(&i)▉

134

Page 135: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Corrija o arquivo incrementa.c

void incrementa(int *i) { ++(*i);}

Reinicie o cling e teste:

[cling]$ .L incrementa.c[cling]$ int i = 0;[cling]$ incrementa(&i)[cling]$ ▉

135

Page 136: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Corrija o arquivo incrementa.c

void incrementa(int *i) { ++(*i);}

Reinicie o cling e teste:

[cling]$ .L incrementa.c[cling]$ int i = 0;[cling]$ incrementa(&i)[cling]$ i▉

136

Page 137: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Corrija o arquivo incrementa.c

void incrementa(int *i) { ++(*i);}

Reinicie o cling e teste:

[cling]$ .L incrementa.c[cling]$ int i = 0;[cling]$ incrementa(&i)[cling]$ i(int) 1[cling]$ ▉

137

Page 138: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

PASSAGEM DE PARÂMETROS

Crie o arquivo ordena.c

Implemente a função de nome troca, que troca os valores de duas variáveis do tipo int.

Exemplo de uso no cling:[cling]$ .L ordena.c[cling]$ int x = 42, y = 99;[cling]$ troca(&x, &y); [cling]$ x(int) 99[cling]$ y(int) 42

138

Page 139: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ORDENAÇÃO POR BORBULHAMENTO (bubble sort)

Entrada: array A de intResultado: ordena A em ordem crescente

• enquanto houver par de elementos A[i], A[i+1] tal que A[i] > A[i+1] (onde i = 0…n-2) • troca A[i] com A[i+1]

Ordenação in-place: o próprio array é ordenado, sem criação de outro array auxiliar.

139

Page 140: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ORDENAÇÃO POR BORBULHAMENTO (bubble sort)

Implemente no arquivo ordena.c:• void bubblesort(int *a, int n)

Use a função troca.

Teste no cling:[cling]$ .L ordena.c[cling]$ int v[] = { 42, 18, 99, -1, 3 };[cling]$ bubblesort(v, 5);[cling]$ v▉

140

Page 141: Programação Estruturada Prof. Rodrigo Hausen http ...compscinet.org/hausen/courses/2016/q3/progest/aulas/aula07/aula07.pdf · int main(void) {char *<strong>p</strong>

ORDENAÇÃO POR BORBULHAMENTO (bubble sort)

Implemente no arquivo ordena.c:• void bubblesort(int *a, int n)

Use a função troca.

Teste no cling:[cling]$ .L ordena.c[cling]$ int v[] = { 42, 18, 99, -1, 3 };[cling]$ bubblesort(v, 5);[cling]$ v(int [5]) { -1, 3, 18, 42, 99 }[cling]$ ▉

141