Foros del Web » Programación para mayores de 30 ;) » C/C++ »

[SOLUCIONADO] Imprimir valores de struct usando punteros

Estas en el tema de Imprimir valores de struct usando punteros en el foro de C/C++ en Foros del Web. Como podría imprimir los valores de un struct usando punteros. Mi primer intento: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código C: Ver original #include <stdio.h> #include <stdlib.h>   struct ...
  #1 (permalink)  
Antiguo 19/05/2013, 13:02
 
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. }
  #2 (permalink)  
Antiguo 19/05/2013, 13:07
 
Fecha de Ingreso: julio-2012
Mensajes: 375
Antigüedad: 11 años, 9 meses
Puntos: 28
Respuesta: Imprimir valores de struct usando punteros

Elimina el char nombre[2]. Te sale más cuenta poner un char nombre a secas.

Los valores se imprimen igual pero tienes que usar el operador -> en vez '.'

Código C:
Ver original
  1. printf("%d",estructura->dato);
  #3 (permalink)  
Antiguo 19/05/2013, 15:56
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Imprimir valores de struct usando punteros

Ok ya entiendo lo que tratas de hacer.

Lo que intentas hacer es un grafo y lo puedes hacer de 2 formas:

1. Usando una matriz de adyacencia
2. Usando una lista de adyacencia

Si estuvieras usando C++ seria mas fácil.

Te dejo un ejemplo de lo que supongo quieres hacer.
Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct barra {
  6.        double fem;
  7.        double df;
  8. };
  9.  
  10. struct barra get_nudo(struct barra matrix[][26], char row, char column) {
  11.     return matrix[row - 'A'][column - 'A'];
  12. }
  13.  
  14. void set_nudo(struct barra matrix[][26], char row, char column, struct barra value) {
  15.     matrix[row - 'A'][column - 'A'].df = value.df;
  16.     matrix[row - 'A'][column - 'A'].fem = value.fem;
  17. }
  18.  
  19. void print_row(struct barra matrix[][26], char row) {
  20.     int i;
  21.     struct barra n;
  22.  
  23.     printf("Fila %c\n", row);
  24.     for(i = 0; i < 26; i++) {
  25.         n = matrix[row - 'A'][i];
  26.         if(n.fem != 0.0 || n.df != 0.0)
  27.             printf("%c%c %.2f %.2f\n", row, 'A'+i, n.fem, n.df);
  28.     }
  29. }
  30.  
  31. void print_column(struct barra matrix[][26], char column) {
  32.     int i;
  33.     struct barra n;
  34.  
  35.     printf("Columna %c\n", column);
  36.     for(i = 0; i < 26; i++) {
  37.         n = matrix[i][column - 'A'];
  38.         if(n.fem != 0.0 || n.df != 0.0)
  39.             printf("%c%c %.2f %.2f\n", 'A'+i, column, n.fem, n.df);
  40.     }
  41. }
  42.  
  43. int main()
  44. {
  45.     int i, j;
  46.     struct barra nudos[26][26], nudo;
  47.  
  48.     memset(nudos, 0, 26 * 26 * sizeof(struct barra));
  49.  
  50.     nudo.fem = 1.0; nudo.df = 2.0;
  51.     set_nudo(nudos, 'A', 'B', nudo);
  52.  
  53.     nudo.fem = 0.613; nudo.df = 0.0;
  54.     set_nudo(nudos, 'B', 'A', nudo);
  55.     nudo.fem = 0.387; nudo.df = -24.0;
  56.     set_nudo(nudos, 'B', 'C', nudo);
  57.  
  58.     nudo.fem = 0.5; nudo.df = 24.0;
  59.     set_nudo(nudos, 'C', 'B', nudo);
  60.     nudo.fem = 0.5; nudo.df = 0.0;
  61.     set_nudo(nudos, 'C', 'D', nudo);
  62.  
  63.     nudo.fem = 1.0; nudo.df = 0.0;
  64.     set_nudo(nudos, 'D', 'C', nudo);
  65.  
  66.     print_row(nudos, 'A');
  67.     print_column(nudos, 'B');
  68.  
  69.  
  70.     return 0;
  71. }
  #4 (permalink)  
Antiguo 20/05/2013, 07:30
 
Fecha de Ingreso: julio-2011
Mensajes: 62
Antigüedad: 12 años, 9 meses
Puntos: 0
Respuesta: Imprimir valores de struct usando punteros

Muchas gracias razpeitia, el programa que estoy escribiendo inicialmente trate de hacerlo con grafos pero no tenia ni idea de como hacerlo.

Etiquetas: int, punteros, struct, usando
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 07:08.