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

Agregar datos en un struct que contiene punteros

Estas en el tema de Agregar datos en un struct que contiene punteros en el foro de C/C++ en Foros del Web. Es posible agregar elementos a un struct que contiene punteros? : @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código C: Ver original #include <stdio.h> #include <stdlib.h>   struct barra { ...
  #1 (permalink)  
Antiguo 20/05/2013, 14:18
 
Fecha de Ingreso: julio-2011
Mensajes: 62
Antigüedad: 12 años, 9 meses
Puntos: 0
Pregunta Agregar datos en un struct que contiene punteros

Es posible agregar elementos a un struct que contiene punteros? :
Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct barra{
  5.        char inicio;// char *nombre;
  6.        char final;
  7.        double *datos;
  8.        };
  9.  
  10. struct nudo{
  11.        char nombre_nudo;
  12.        struct barra *elemento;
  13.        };
  14.  
  15. int main()
  16. {
  17.     struct nudo *nudos;
  18.     double femdf[] = {0.387, -24.0};
  19.     nudos = (struct nudo *) malloc (sizeof (struct nudo));
  20.     nudos->nombre_nudo = 'B';
  21.     nudos->elemento->inicio = 'B';
  22.     nudos->elemento->final = 'C';
  23.     nudos->elemento->datos = femdf;
  24.     nudos->elemento->datos[2] = 9.288;// agregando un elemento ???
  25.     printf("%f\n", *(nudos->elemento->datos + 0));
  26. //    struct nudo nudos[] = {
  27. //           { 'A', { {'A', 'B', {0.0, 0.0}} } },
  28. //           { 'B', { {'B', 'A', {0.613, 0.0}}, {'B', 'C', {0.387, -24.0}} } },
  29. //           { 'C', { {'C', 'B', {0.5, 24.0}}, {'C', 'D', {0.5,0.0}} } },
  30. //           { 'D', { {'D', 'C', {1.0, 0.0}} } },
  31. //           };
  32. //    Antes de calcular:
  33. //    A = {'A', 'A', 'B', {0.0, 0.0}};
  34. //    B = {'B', 'B', 'A', {0.613, 0.0}};
  35. //    C = {'B', 'B', 'C', {0.387, -24.0}};
  36. //    D = {'C', 'C', 'B', {0.5, 24.0}};
  37. //    E = {'C', 'C', 'D', {0.5,0.0}};
  38. //    F = {'D', 'D', 'C', {1.0, 0.0}};
  39. //    Después de calcular:
  40. //    A = {'A', 'A', 'B', {0.0, 0.0, 7.356, 0.0, 1.839, 0.0, 0.356, 0.0, ...}};
  41. //    B = {'B', 'B', 'A', {0.613, 0.0, 14.712, 0.0, 3.678, 0.0, 0.712, ...}};
  42. //    C = {'B', 'B', 'C', {0.387, -24.0, 9.288, -6.00, 2.322, -1.161, ...}};
  43. //    D = {'C', 'C', 'B', {0.5, 24.0, ...}};
  44. //    E = {'C', 'C', 'D', {0.5,0.0, ...}};
  45. //    F = {'D', 'D', 'C', {1.0, 0.0, ...}};
  46.  
  47.     system("PAUSE");
  48.     return 0;
  49. }
  #2 (permalink)  
Antiguo 20/05/2013, 14:56
 
Fecha de Ingreso: julio-2012
Mensajes: 375
Antigüedad: 11 años, 9 meses
Puntos: 28
Respuesta: Agregar datos en un struct que contiene punteros

Te refieres a usar malloc en un elemento de una estructura?

Sí claro.

Etiquetas: int, punteros, struct
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 22:41.