Created
March 5, 2019 00:36
-
-
Save romicofre/43e720ecf7dff571ad69fecde3cd24dc to your computer and use it in GitHub Desktop.
Estructuras en C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| struct point{ | |
| int x; | |
| int y; | |
| }; | |
| typedef struct{ //creando un nuevo tipo para coordenada del punto | |
| int y; | |
| int x; | |
| }punto; | |
| typedef struct{ //rectangulo con dos puntos | |
| punto p1; | |
| punto p2; | |
| }rect; | |
| int main(){ | |
| int i; | |
| struct point p1= {3,4}; | |
| punto p2 = {4,3}; | |
| rect r; | |
| rect *R, *R2; | |
| // Podemos crear un arreglo de puntos | |
| punto puntos[2]; | |
| for (i=0; i<2; i++){ | |
| puntos[i].x=i; | |
| puntos[i].y=i; | |
| printf("%d: (%d, %d)\n", i, puntos[i].x, puntos[i].y); | |
| } | |
| // Pero tambien podria ser un puntero de puntos | |
| // Si nos acercamos a las listas | |
| // Creando un rectangulo | |
| r.p1.x = 3; | |
| r.p1.y = 3; | |
| r.p2.x = 6; | |
| r.p2.y = 5; | |
| printf("rect = (%d, %d),(%d, %d)\n", r.p1.x, r.p1.y, r.p2.x, r.p2.y); | |
| // Creando un puntero de rectangulo | |
| R2 = &r; // puntero R2 apunta a la direccioin de r | |
| //printf("rect = (%d, %d),(%d, %d)\n", *(R2).p1.x, *(R2).p1.y, *(R2).p2.x, *(R2).p2.y); | |
| printf("rect = (%d, %d),(%d, %d)\n", R2->p1.x, R2->p1.y, R2->p2.x,R2->p2.y); | |
| /* | |
| R.p1.x = 3; | |
| R.p1.y = 3; | |
| R.p2.x = 6; | |
| R.p2.y = 5; | |
| */ | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment