Exforsys

Free Training

Struct as an array of pointers to struct

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 ...

Go Back   Exforsys > Programming Talk > C and C++

Exforsys.com


C and C++ C and C++

Reply

 

LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 09-29-2009, 06:06 PM
Banned
 
Join Date: Sep 2009
Posts: 4
galileopy is on a distinguished road
Exclamation Struct as an array of pointers to struct

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. I tried to use array notation.
2. Also using the pointer arithmetic, but it is still crashing

Thanks a lot, meanwhile I'll be trying to help someone else xD

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; // here it is, this is the one that's not working
};
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));// No warnings from the compiler here.
	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
3.-------CODE-------
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)); // When here it crashes
	}
	
}
#endif
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Struct as an array of pointers to struct galileopy C and C++ 0 09-29-2009 05:56 PM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-15-2004 06:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-01-2004 06:01 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 06-01-2004 06:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 05-15-2004 06:00 AM


All times are GMT -4. The time now is 04:12 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
Copyright 2004 - 2009 Exforsys Inc. All rights reserved.