This is a discussion on Struct as an array of pointers to struct within the C and C++ forums, part of the Programming Talk category; Here it goes. I have a little problem when trying to use the struct proyecto_t, The code 1 below is ...
|
|||||||
|
|||
|
Here it goes.
I have a little problem when trying to use the struct proyecto_t, The code 1 below is a copy of my structures file. Then there's a copy of one of the procedures that is working fine with a proyecto_t variable. That's code 2. But when I try to access a element pointed by PRO->actividades[i]->dur, that is a unsigned int with scanf(), my program crashes... I did try to use the address of (&) operator and also tried using it twice but nothing happens it is still crashing at runtime. Code 3 is the copy of the procedures that fails. 1.---------CODE--------------- Code:
#ifndef _ST_ACTIVIDAD_
#define _ST_ACTIVIDAD_
struct actividad
{
char name[15];
unsigned int dur;
};
typedef struct actividad actividad_t;
#endif
#ifndef _ST_PROYECTO_
#define _ST_PROYECTO_
struct proyecto
{
unsigned int n;
actividad_t *actividades; :icon14::icon14:
};
typedef struct proyecto proyecto_t;
#endif
#ifndef _ST_RELACION_
#define _ST_RELACION_
struct relacion
{
short **relaciones; // THIS WORKS FINE AS A MATRIX
};
typedef struct relacion relacion_t;
#endif
2.------CODE------------ Code:
#ifndef P_INI_ST
#define P_INI_ST
void ini(proyecto_t *PRO, relacion_t *REL)
{
unsigned int
i;
PRO->actividades = (actividad_t*) malloc(sizeof(actividad_t)*(PRO->n));:icon14::icon14:
REL->relaciones= (short**) malloc(sizeof(short*)*PRO->n);
for(i=0;i<PRO->n;i++)
{
REL->relaciones[i]= (short*) malloc(sizeof(short)*PRO->n);
}
}
#endif
Code:
#ifndef P_LEER_PROYECTO_ST
#define P_LEER_PROYECTO_ST
void leer_proyecto(proyecto_t *PRO, relacion_t *REL)
{
unsigned int
i;
for(i=0;i<PRO->n;i++)
{
printf("Ingrese actividad: ");
scanf("%[^\n]s",&(PRO->actividades[i]->name));
printf("Ingrese duracion: ");
scanf("%u",&(PRO->actividades[i]->dur));:icon14::icon14:
}
}
#endif
|
| Sponsored Links |
![]() |
| Bookmarks |
| Tags |
| array, pointer, scanf()., struct |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) | Steve Summit | Tech FAQ | 0 | 06-15-2004 07:00 AM |
| comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) | Steve Summit | Tech FAQ | 0 | 06-01-2004 07:01 AM |
| comp.lang.c FAQ list Table of Contents | Steve Summit | Tech FAQ | 0 | 06-01-2004 07:00 AM |
| comp.lang.c Answers to Frequently Asked Questions (FAQ List) | Steve Summit | Tech FAQ | 0 | 06-01-2004 07:00 AM |
| comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) | Steve Summit | Tech FAQ | 0 | 05-15-2004 07:00 AM |