Beruflich Dokumente
Kultur Dokumente
Registro - Structs
Exerccios
Atividade de laboratrio
Structs
Uma struct uma coleo de um ou mais valores
agrupados sob um nico identificador.
struct nome_da_estrutura{
tipo var1;
tipo var2;
...
tipo varN;
};
Membros: so variveis de
tipos bsicos, vetores,
strings ou outras structs.
Declaraes Combinadas
struct Aluno{
int matricula;
float nota[3];
float media;
}PE, ICC, LPI;
Operador typedef
Exemplo
typedef unsigned int uint;
int main(){
uint x;
...
}
struct Aluno{
int matricula;
float nota[3];
float media;
};
Aluno ICC;
typedef struct{
int matricula;
float nota[3];
float media;
}Aluno;
Aluno ICC;
//Erro
Structs Aninhadas
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int dia;
char ms[10];
int ano;
}Data;
typedef struct{
int pecas;
float preco;
Data diavenda;
}Venda;
Structs Aninhadas
int main(){
static Venda A = {20, 110.0, {23, maro, 2012}};
printf(Peas: %d\n, A.pecas);
printf(Preco: %.2f\n, A.preco);
printf(Data: %d de %s de %d\n, A.diavenda.dia,
A.diavenda.mes, A.diavenda.ano);
system(pause);
return 0;
}
Vetor de Structs
Um vetor de structs tem por objetivo armazenar conjuntos de
elementos complexos.
Declarao de um vetor de structs
Exemplo
vendas[n].preco;
struct tipo_endereco
{
char rua [50];
int numero;
char bairro [20];
char cidade [30];
char sigla_estado [3];
};
struct ficha_pessoal
{
char nome [50];
unsigned int telefone[10];
struct tipo_endereco endereco;
};
main (void)
{
struct ficha_pessoal ficha;
strcpy (ficha.nome,"Luiz Osvaldo Silva");
ficha.telefone=4921234;
strcpy(ficha.endereco.rua,"Rua das Flores");
ficha.endereco.numero=10;
main (void)
{
struct ficha_pessoal ficha[10];
Exerccios
26
1)
b) livro;
c) aluno;
d) endereo;
e) cadastro de pessoa;
Exerccios
27
2)
3)