p. bosch
p. bosch

Reputation: 139

Errors while creating a graph in C

I'm trying to generate a graph from a matrix but I'm having some problems, here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_N 10
#define MAX_P 20

//Declaracion del TDA cola
typedef struct Node {
   int rafaga;
   int id;
   struct Node *dret;
   struct Node *esq;
   int fiscals;
   int funcionaris;
   int advocats;
} tipoNodo;

typedef tipoNodo *pNodo; // tipo para declarar nodos a un entero

int nodes = 0, funcionarisTotals = 0, advocatsTotals = 0, fiscalsTotals = 0;
Node llista[MAX_N];
int graf[MAX_N*2][3];

void crearGraf(int graf[MAX_N*2][3]) {
    FILE *pf;
    char caracter; // variable de tipo caracter que va a servir para almacenar los caracteres leidos del archivo
    int valor, o, d;
    pf = fopen ("graf.txt","r"); // se abre el archivo en forma de lectura
    if (!pf) {  //en el caso que no se pueda abrir el archivo se manda un mensaje de error
        printf ("ERROR: el fichero  no existe o no se puede abrir\n");
        exit(-1); //mensaje "presiona una tecla para continuar"
    }
    else {
        int arestes = 0;
        while (!feof (pf)) {
            int j = 0;
            nodes = int ((caracter=fgetc(pf))-'0'); 
            printf ("El graf te %d\n nodes", nodes);
            for(int i = 0 ; i < nodes ; i++) {
                llista->[i].id = i;
                llista.[i].rafaga = 1;
            }
            caracter = fgetc(pf);
            o = int ((caracter = fgetc(pf))-'0'); //nodo origen
            caracter = fgetc(pf);
            d = int ((caracter = fgetc(pf))-'0');//nodo destino
            caracter = fgetc(pf);
            valor = int ((caracter =fgetc(pf))-'0');//pes de l'aresta
            if (o < 0 || d < 0 || valor < 0) break;
            printf ("%d %d %d\n",o,d,valor);
            graf[o][d] = valor; // es guarda el pes de l'aresta que va de X -> Y a la matriu
            arestes++;
        }
        fclose (pf);//se cierra el fichero
        for(int j = 0; j<arestes ; j++) {
            if(j == 0) {
                llista[graf[j][0]]->dret = llista[graf[j][1]]; // enllacem l'aresta primera
            } 
            if(j != 0 && (graf[j-1][0] == graf[j][0])) {
                llista[graf[j][0]]->esq = llista[graf[j][1]]; // enllacem l'aresta esquerra
            }
            else {
                llista[graf[j][0]]->dret = llista[graf[j][1]]; // enllacem l'aresta dreta
            }
        }
    }
}

int main (void)
{
    int graf[MAX_N][MAX_N];
    crearGraf(graf);
    return 0;
}

The error I'm getting is:

SC.c:57: error: expected unqualified-id before ‘[’ token
SC.c:58: error: expected unqualified-id before ‘[’ token

Which are these two lines in the for loop:

for(int i = 0 ; i < nodes ; i++) {
                llista->[i].id = i;
                llista.[i].rafaga = 1;
            }

I think I don't understand really the TYPE of variable I'mworking with here. Could someone give me a hand? I've been trying to look for similar problems but I couldn't fix mine.

Thanks!

Upvotes: 1

Views: 37

Answers (1)

mathematician1975
mathematician1975

Reputation: 21351

Change these 2 lines to

llista[i].id = i;
llista[i].rafaga = 1;

and you will be accessing the things that you appear to be trying to access.

Using the -> operator dereferences a pointer while it appears that you just want to access array elements which is done as above. I cannot speak for the correctness of the remainder of your code but these changes at the least should remove your compiler errors.

Upvotes: 1

Related Questions