Академический Документы
Профессиональный Документы
Культура Документы
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Definição de Tipos
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Agregados Heterogêneos
Struct: conjunto de dados heterogêneos
referenciados por um único nome e
individualizados por campos
– cada campo ocupa um número inteiro de unidades
endereçáveis de memória, podendo ser referenciado
pelo seu nome
– normalmente os elementos em uma estrutura estão
logicamente relacionados
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Estruturas
Sintaxe:
struct nome_estrutura {
tipo_1 dado_1;
tipo_2 dado_2;
...
tipo_n dado_n;
} lista_de_variaveis;
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Estruturas
Sintaxe para apenas declarar variável:
struct {
tipo_1 dado_1;
tipo_2 dado_2;
...
tipo_n dado_n;
} lista_de_variaveis;
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Estruturas
Sintaxe nomeando a estrutura e depois
criando variáveis:
struct nome_estrutura {
tipo_1 dado_1;
tipo_2 dado_2;
...
tipo_n dado_n;
};
struct nome_estrutura lista_de_variáveis;
6
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Estruturas
Exemplo:
Criando uma variável Utilização
struct {
int registro;
char nome[50]; func.idade = 31;
int idade; strcpy(func.nome,“Luiz”);
float salario;
} func;
7
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Estruturas
Exemplo:
Nomeando a estrutura Utilização
struct tipo_func {
int registro;
func2.idade=func.idade;
char nome[50];
int idade;
float salario;
} func;
struct tipo_func func2;
8
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Definindo um Tipo
typedef struct {
int registro;
char nome[50];
int idade;
float salario;
} TIPO_FUNC;
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Estruturas
Variações:
– Estrutura de Estruturas
– Vetor de Estruturas
10
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Variações de Estruturas
Contendo Estruturas Vetor de Estruturas
struct tipoend { struct tipoend {
char endereco[40]; char endereco[40];
char cep[9]; char cidade[25];
char cidade[25]; };
}; struct tipopessoa {
struct tipopessoa { char nome[15];
char nome[15]; struct tipoend end;
struct tipoend end; };
float salario; struct tipopessoa pessoa[50];
};
struct tipopessoa pessoa; /* cidade da 13a pessoa:
11 pessoa[12].end.cidade */
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Ponteiros para Estruturas
Sintaxe:
struct nome_estrutura {
tipo1 dado1;
tipo2 dado2; var.dado1
... equivale a
tipon dadon; ptr->dado1
} var, *ptr;
12
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Ponteiros para Estruturas
Exemplo: Acessando o no da conta do
cliente:
typedef struct {
int num; c.num=120;
char nome[80]; pc = &c;
float saldo; printf("Conta: %d\n",c.num);
} TIPOCLI; printf("Conta: %d \n", pc->num);
TIPOCLI c, *pc; printf("Conta: %d \n",(*pc).num);
13
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Estruturas como Parâmetros
main( ) {
Exemplo: tipocli cliente;
Rendimento do Saldo printf("Informe saldo atual: ");
Passagem por Referência scanf("%f",&cliente.saldo);
de um elemento do vetor
rende(&cliente);
printf("\nNovo saldo: ");
#include "stdio.h"
printf("%0.2f \n",cliente.saldo);
typedef struct{ }
int reg; void rende( tipocli *cli) {
char nome[80]; float taxa;
float saldo; printf("\nInforme rendimento: ");
} tipocli ; scanf("%f",&taxa);
cli -> saldo *= taxa;
14 void rende(tipocli *cli);
}
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)
Estruturas como Parâmetros
main( ) {
Exemplo: tipocli cliente[3]; int i;
Rendimento do Saldo for (i=0; i<3; i++) {
Passagem por Referência printf("Informe saldo atual: ");
do vetor inteiro scanf("%f",&cliente[i].saldo);
}
rende(cliente);
#include "stdio.h" for (i=0; i<3; i++) {
typedef struct { printf("\nNovo saldo: ");
int reg; printf("%0.2f \n",cliente[i].saldo);
} void rende(cli)
char nome[80]; }
tipocli cli[];
float saldo; void rende(tipocli *cli) { {
float taxa; int i;
}tipocli;
printf("\nInforme rendimento: ");
void rende(tipocli *cli); scanf("%f",&taxa);
for (i=0; i<3; i++)
15
cli[i] . saldo *= taxa; Não usa “->”
}
You created this PDF from an application that is not licensed to print to novaPDF printer (http://www.novapdf.com)