Code problems from a C priority list

Asked

Viewed 235 times

0

I am creating a priority list in C (topological ordering) using vectors and within these vectors a chained list pointing out who has to come before whom. It’s just that there’s a glitch in the pointer and I really can’t figure it out.

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#define DIM 50

typedef struct list{
    struct list * prox;
    int contador;
}LISTA;

typedef struct ant{
    struct ant* prox;
    int chave;
}PRE;

void iniciar(int n, int m, LISTA lista[DIM], PRE *ptpre);

int main(void){
    LISTA lista[DIM],pt;
    PRE ptpre;

    int m,n,fim,objeto,indice,i;
    scanf("%d %d",&n,&m);
    iniciar(n,m,lista,&ptpre);
    fim = 0;
    lista[0].contador=0;
    for (i=1;i<=n;i++){
        if(lista[i].contador==0){
            lista[fim].contador = i;
            fim = i;
        } 
    }
    objeto=lista[0].contador;
    while(objeto != 0){
        printf("%d",objeto);
        pt = lista[objeto].prox;
        while (*pt != 0){
            indice = pt->chave;
            lista[indice].contador = lista[indice].contador -1;
            if (lista[indice].contador == 0){
                lista[fim].contador = indice;
                fim = indice;
            }
            pt = pt->prox;
        }
      objeto=lista[objeto].contador; 
    }
return 0;    
}

void iniciar(int n, int m, LISTA lista[DIM], PRE *ptpre){
    int i;
    for (i=0;i<=n;i++){
        lista[i].contador = 0;
        lista[i].prox = 0;
    }
    for (i=1;1<=m;i++){
        scanf("%d %d",&a,&b);
        ptpre = (PRE *) malloc(sizeof(PRE));
        ptpre -> chave = b;
        ptpre -> prox = lista[a].prox;
        lista[a].prox = ptpre;
        lista[b].contador = lista[b].contador + 1;
    }


}
  • 1

    Can show error?

  • source_file. c: In Function ːmain': source_file. c:36:12: error: incompatible types when assigning to type ːLISTA {aka struct list}' from type ːstruct list *' pt = list[object]. Prox;

1 answer

0

In

LISTA lista[DIM],pt;

you are creating pt as a variable LISTA, and not as a pointer for a variable LISTA. That’s why you can’t pass lista[objeto].prox for pt, because prox is a pointer and pt is any variable. To fix this problem, change the pt statement to

LISTA lista[DIM], *pt;

I also noticed other errors in your code, such as trying to access a variable chave within the struct pt. That’s impossible, because pt is a LISTA, and the only struct in your code you have a member called chave is the struct PRE.

  • Hello friend, I made the changes you said. I changed from *pt to *ptpre so the problem you’re giving yourself is when it comes to creating a pointer that points to the structure itself. error saying the type of pointer *ptpre is different from the structure pointer so it makes no sense since *ptpre is the same type of structure I want to point to

Browser other questions tagged

You are not signed in. Login or sign up in order to post.