简体   繁体   English

无法在C中的表中分配指针

[英]Trouble assigning pointers from a table in C

I'm trying to create a graph from a file, I manage to read the file and save all the info in a matrix, however, when I try to read from the matrix and link the different nodes between them I get the segmentation fault error. 我正在尝试从文件创建图形,我设法读取文件并将所有信息保存在矩阵中,但是,当我尝试从矩阵读取并链接它们之间的不同节点时,出现分段错误错误。

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;
        nodes = int ((caracter=fgetc(pf))-'0'); 
        printf ("El graf te %d nodes\n", nodes);
        printf("abans del for de les rafagues\n");
        for(int i = 0 ; i < nodes ; i++) {
            printf("abans de posar la id\n");
            llista[i].id = i;
            printf("abans de posar la rafaga\n");
            llista[i].rafaga = 1;
        }
        printf("despres del for de les rafagues\n");
        while (!feof (pf)) {
            int j = 0;
            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[arestes][0] = o; // es guarda el pes de l'aresta que va de X -> Y a la matriu
            graf[arestes][1] = d;
            graf[arestes][2] = valor;
            printf("He llegit l'aresta %d\n", arestes);
            arestes++;
        }
        printf("Fora del while de llegir fitxer\n");
        fclose (pf);//se cierra el fichero
        printf("Abans del for de les arestes, fare %d voltes\n",arestes);
        for(int j = 0; j<arestes ; j++) {
        printf("Posant l'aresta del node %d al %d\n", graf[j][0],graf[j][1]);
            if(j == 0) {
                printf("posant la primera aresta de pes %d\n",graf[j][2]);
                *llista[graf[j][0]].dret = llista[graf[j][1]]; // enllacem l'aresta primera
                printf("primera aresta posada\n");
            } 
            if(j != 0 && (graf[j-1][0] == graf[j][0])) {
                printf("posant l'aresta del node %d and node %d com a fill esquerra\n",graf[j][0],graf[j][1]);
                *llista[graf[j][0]].esq = llista[graf[j][1]]; // enllacem l'aresta esquerra
            }
            else {
                printf("posant l'aresta del node %d and node %d com a fill dret\n",graf[j][0],graf[j][1]);
                *llista[graf[j][0]].dret = llista[graf[j][1]]; // enllacem l'aresta dreta
            }
        }
    }
}

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

The error I'm getting is when I try to link the first edge of the graph, in this line: 我收到的错误是当我尝试在此行中链接图形的第一条边时:

*llista[graf[j][0]].dret = llista[graf[j][1]]; // enllacem l'aresta primera

I think I'm messing up the type of the different variables. 我认为我搞砸了不同变量的类型。 Also, all the printfs you see are to confirm that everything works just fine untill this point. 另外,您看到的所有printfs都是为了确认一切正常,直到这一点为止。

What am I missing again? 我又想念什么?

Thanks guys! 多谢你们!

You are de-referencing a null pointer when you *llista[graf[j][0]].dret (because dret is a pointer that is not set yet..) 当您使用*llista[graf[j][0]].dret时,您正在取消引用空指针(因为dret是尚未设置的指针。)

i think what you meant to do is: 我认为您的意思是:

llista[graf[j][0]].dret = &llista[graf[j][1]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM