Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/05/2013, 13:02
Bael_Balzac
 
Fecha de Ingreso: julio-2011
Mensajes: 62
Antigüedad: 12 años, 9 meses
Puntos: 0
Pregunta Imprimir valores de struct usando punteros

Como podría imprimir los valores de un struct usando punteros.
Mi primer intento:
Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct barra{
  5.        char nombre[3];
  6.        double fem;
  7.        double df;
  8. };
  9.  
  10. struct nudo{
  11.        char nombre[2];
  12.        struct barra elemento;
  13. };
  14.  
  15. int main()
  16. {
  17.     int i,j;
  18.     struct nudo nudos[] = {
  19.            {"A", {"AB", 0.0, 0.0}},
  20.            {"B", {"BA", 0.613, 0.0}},
  21.            {"B", {"BC", 0.387, -24.0}},
  22.            {"C", {"CB", 0.5, 24.0}},
  23.            {"C", {"CD", 0.5,0.0}},
  24.            {"D", {"DC", 1.0, 0.0}},
  25.            };
  26.     struct barra barras[] = {{"AB", 1.0, 2.0}, {"AC", 3.0, 4.0}};
  27.     struct nudo *dato1;
  28.     struct barra *dato2;
  29.     dato1 = nudos;
  30.     dato2 = barras;
  31.     for(j=0;i<6;j++)
  32.     {
  33.         printf("%s\n", dato1->nombre);
  34.         printf("%s\n", (dato1->elemento).nombre);
  35.         printf("%f\n", (dato1->elemento).fem);
  36.         printf("%f\n", (dato1->elemento).df);
  37.         printf("\n");
  38.         *dato1++;
  39.     }
  40.     for(i=0;i<2;i++)
  41.     {
  42.         printf("%s\n", dato2->nombre);  
  43.         printf("%f\n", dato2->fem);    
  44.         printf("%f\n", dato2->df);
  45.         printf("\n");
  46.         *dato2++;
  47.     }
  48.     system("PAUSE");
  49.     return 0;
  50. }
Mi segundo intento:
Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct barra{
  5.        char nombre[3];
  6.        double fem;
  7.        double df;
  8. };
  9.  
  10. struct nudo{
  11.        char nombre[2];
  12.        struct barra elemento[];
  13. };
  14.  
  15. int main()
  16. {
  17.     int i,j;
  18.     struct nudo nudos[] = {
  19.            {"A", {{"AB", 0.0, 0.0}}},
  20.            {"B", {{"BA", 0.613, 0.0}, {"BC", 0.387, -24.0}}},
  21.            {"C", {{"CB", 0.5, 24.0}, {"CD", 0.5,0.0}}},
  22.            {"D", {{"DC", 1.0, 0.0}}},
  23.            };
  24.     struct barra barras[] = {{"AB", 1.0, 2.0}, {"AC", 3.0, 4.0}};
  25.     struct nudo *dato1;
  26.     struct barra *dato2;
  27.     dato1 = nudos;
  28.     dato2 = barras;
  29.     for(j=0;i<6;j++)
  30.     {
  31.         printf("%s\n", dato1->nombre);
  32.         printf("%s\n", (dato1->elemento).nombre);
  33.         printf("%f\n", (dato1->elemento).fem);
  34.         printf("%f\n", (dato1->elemento).df);
  35.         printf("\n");
  36.         *dato1++;
  37.     }
  38.     for(i=0;i<2;i++)
  39.     {
  40.         printf("%s\n", dato2->nombre);  
  41.         printf("%f\n", dato2->fem);    
  42.         printf("%f\n", dato2->df);
  43.         printf("\n");
  44.         *dato2++;
  45.     }
  46.     system("PAUSE");
  47.     return 0;
  48. }
Mi tercer intento usando puntero a puntero:
Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct barra{
  5.        char nombre[3];
  6.        double fem;
  7.        double df;
  8. };
  9.  
  10. struct nudo{
  11.        char nombre[2];
  12.        struct barra elemento[];
  13. };
  14.  
  15. int main()
  16. {
  17.     struct barra barra1[] = {{"AB", 1.0, 2.0}};
  18.     struct barra barra2[] = {{"BA", 0.613, 0.0}, {"BC", 0.387, -24.0}};
  19.     struct barra barra3[] = {{"CB", 0.5, 24.0}, {"CD", 0.5,0.0}};
  20.     struct barra barra4[] = {{"DC", 1.0, 0.0}};
  21.     struct barra *a, *b, *c, *d;
  22.     a = barra1;
  23.     b = barra2;
  24.     c = barra3;
  25.     d = barra4;
  26.     struct nudo nudo[] = {{"A", barra1}, {"B", barra2}, {"C", barra3}, {"D", barra4}};
  27.     struct nudo *e,
  28.     e = nudos
  29.     system("PAUSE");
  30.     return 0;
  31. }