Zero-Giulio
09-12-2008, 20:40
Non programmo da un bel po di tempo e non ricordo come si usano le variabili globali.
Ho scritto qualcosa ma ottengo errori di compilazione (uso DevC++ o Code::Blocks).
Allora... Ho creato un progetto. Ci stà il .h
#include <stdio.h>
#include <stdlib.h>
struct nodes
{
double x;
double y;
double z;
};
struct edges
{
long int first_node;
long int second_node;
};
typedef struct nodes nodes;
typedef struct nodes *nodesptr;
typedef struct edges edges;
typedef struct edges *edgesptr;
nodesptr nodes_list=NULL;
edgesptr edges_list=NULL;
int read_nodes_file (char *);
un .c
#include "whitney.h"
extern nodesptr nodes_list;
extern edgesptr edges_list;
int read_nodes_file (char nodes_file_name[])
{
long int i;
long int cont, nodes_num;
double x, y, z;
FILE *nodes_file;
nodes_file=fopen (nodes_file_name, "r");
if (nodes_file == NULL)
{
printf ("\nError: can't open file %s.\n", nodes_file_name);
return 0;
}
fscanf (nodes_file, "%ld", &nodes_num);
nodes_list=(nodesptr) malloc ((nodes_num+1)*sizeof (nodes));
nodes_list[0].x=nodes_list[0].y=nodes_list[0].z=(double) nodes_num;
for (i=1; i<=nodes_num; i++)
{
fscanf (nodes_file, "%ld %lf %lf %lf", &cont, &nodes_list[i].x, &nodes_list[i].y, &nodes_list[i].z);
if (cont != i)
{
printf ("\nError: in line %ld of file %s.\n", i, nodes_file_name);
return 0;
}
}
fclose (nodes_file);
return 1;
}
e un secondo .c col main:
#include "whitney.h"
int main ()
{
int logic=0;
char nodes_file_name [40];
printf ("\nIntroduce the name of the file with the list of the nodes: \n\n\t");
scanf ("%s", nodes_file_name);
logic=read_nodes_file (nodes_file_name)
}
Come dicevo ho errori di compilazione. Dice che ci sono dichiarazioni multiple di nodes_list e di edges_list.
Non so come risolvere.
In pratica: ho due strutture, nodes e edges, e voglio due vettori globali di queste struct globali, perchè voglio utilizzarle ovunque (in tutte le funzioni di tutti i files (che saranno più dei due che ho messo sopra)). Come faccio?
Ho scritto qualcosa ma ottengo errori di compilazione (uso DevC++ o Code::Blocks).
Allora... Ho creato un progetto. Ci stà il .h
#include <stdio.h>
#include <stdlib.h>
struct nodes
{
double x;
double y;
double z;
};
struct edges
{
long int first_node;
long int second_node;
};
typedef struct nodes nodes;
typedef struct nodes *nodesptr;
typedef struct edges edges;
typedef struct edges *edgesptr;
nodesptr nodes_list=NULL;
edgesptr edges_list=NULL;
int read_nodes_file (char *);
un .c
#include "whitney.h"
extern nodesptr nodes_list;
extern edgesptr edges_list;
int read_nodes_file (char nodes_file_name[])
{
long int i;
long int cont, nodes_num;
double x, y, z;
FILE *nodes_file;
nodes_file=fopen (nodes_file_name, "r");
if (nodes_file == NULL)
{
printf ("\nError: can't open file %s.\n", nodes_file_name);
return 0;
}
fscanf (nodes_file, "%ld", &nodes_num);
nodes_list=(nodesptr) malloc ((nodes_num+1)*sizeof (nodes));
nodes_list[0].x=nodes_list[0].y=nodes_list[0].z=(double) nodes_num;
for (i=1; i<=nodes_num; i++)
{
fscanf (nodes_file, "%ld %lf %lf %lf", &cont, &nodes_list[i].x, &nodes_list[i].y, &nodes_list[i].z);
if (cont != i)
{
printf ("\nError: in line %ld of file %s.\n", i, nodes_file_name);
return 0;
}
}
fclose (nodes_file);
return 1;
}
e un secondo .c col main:
#include "whitney.h"
int main ()
{
int logic=0;
char nodes_file_name [40];
printf ("\nIntroduce the name of the file with the list of the nodes: \n\n\t");
scanf ("%s", nodes_file_name);
logic=read_nodes_file (nodes_file_name)
}
Come dicevo ho errori di compilazione. Dice che ci sono dichiarazioni multiple di nodes_list e di edges_list.
Non so come risolvere.
In pratica: ho due strutture, nodes e edges, e voglio due vettori globali di queste struct globali, perchè voglio utilizzarle ovunque (in tutte le funzioni di tutti i files (che saranno più dei due che ho messo sopra)). Come faccio?