1. #include <stdio.h> #include <stdlib.h> //* * ESTRUCTURAS * * //Declaramos la estructura struct datos { int codigo; char nombre[15]; }; int main(int argc, char *argv[]) { //Variable struct datos persona ; //PARA METER VALORES printf("Introduce el codigo:"); scanf("%d", &persona.codigo ); fflush(stdin); printf("Introduce el nombre:"); gets( persona.nombre ); system("PAUSE"); return 0; } #include <stdio.h> #include <stdlib.h> //* * ESTRUCTURAS CON PUNTEROS * * //Declaramos la estructura struct datos { int codigo; char nombre[15]; }; int main(int argc, char *argv[]) { //Variable struct datos persona ; //Puntero struct datos * p ; //Inicializar puntero p = &per ; //PARA METER VALORES printf("Introduce el codigo:"); scanf("%d", &p->codigo ); fflush(stdin); printf("Introduce el nombre:"); gets( p->nombre ); system("PAUSE"); return 0; }
2. #include <stdio.h> #include <stdlib.h> //* *ARRAYS DE ESTRUCTURAS CON PUNTEROS * * //Declaramos la estructura struct datos { int codigo; char nombre[15]; }; int main(int argc, char *argv[]) { int i; struct datos array[3]; //Puntero struct datos *p ; //Inicializar puntero p = array ; //RECORRER ARRAY PARA METER VALORES for(i=0;i<3;i++) { printf("Introduce el codigo:"); scanf("%d", &(p+i)->codigo ); fflush(stdin); printf("Introduce el nombre"); gets( (p+i)->nombre ); } system("PAUSE"); return 0; } #include <stdio.h> #include <stdlib.h> //* * ESTRUCTURAS CON PUNTEROS * * //Declaramos la estructura struct datos { int codigo; char nombre[15]; }; int main(int argc, char *argv[]) { struct datos per ; //Puntero struct datos *p ; //Inicializar puntero p =& per ; //PARA METER VALORES printf("Introduce el codigo:"); scanf("%d", &p->codigo ); fflush(stdin); printf("Introduce el nombre:"); gets( p->nombre ); system("PAUSE"); return 0; }
3. #include <stdio.h> #include <stdlib.h> #define t 3 struct datos { int codigo; char nombre[15]; }; void LEER(struct datos *p) { int i; printf("* * LEER VALORES EN LA ESTRUCTURA * *"); for(i=0;i<t;i++) { printf("Codigo: "); scanf("%d ",&(p+i)->codigo ); printf("Nombre: "); fflush(stdin); gets( (p+i)->nombre ); } } void IMPRIMIR(struct datos *p) { int i; printf("* * IMPRIMIR EL CONTENIDO DE LA ESTRUCTURA * *"); for (i=0;i<t;i++) { printf("Codigo: %d ",(p+i)->codigo ); printf("Nombre: "); puts( (p+i)->nombre ); } } int main(int argc, char *argv[]) { //array de estructuras struct datos array[t]; //LLamada a las funciones LEER(array); IMPRIMIR(array); system("PAUSE"); return 0; } #include <stdio.h> #include <stdlib.h> #define t 3 struct datos { int codigo; char nombre[15]; }; void LEER(struct datos *p) { printf("* * LEER VALORES EN LA ESTRUCTURA * *"); printf("Introduce el codigo: "); scanf("%d", &p->codigo ); printf("Introduce el nombre: "); fflush(stdin); gets( p->nombre ); } void IMPRIMIR(struct datos *p) { printf("* * IMPRIMIR EL CONTENIDO DE LA ESTRUCTURA * *"); printf("Codigo: %d“, p->codigo ); printf("Nombre: "); puts( p->nombre ); } int main(int argc, char *argv[]) { //Variable de estructuras struct datos persona ; //LLamada a las funciones LEER(&persona); IMPRIMIR(&persona); system("PAUSE"); return 0; } Pasar una estructura a una funión Pasar un array de estructuras a una funión