Error referencing struct with pointers

0

I'm trying to convert this algorithm down into language c but I having trouble calling the variable and assigning a value to the p.key = 1 of the struct element.

Error Code:

error: request for member 'key' in something not a structure or union

Algorithm

Programa Ponteiro_L5_EX01;
Tipo
Ponteiro = ^Elemento;
Elemento = Registro
           chave : Inteiro;
           Prox  : Ponteiro
           fim;
Var p,prim,h,q  : Ponteiro
    i           : Inteiro

Início
prim <- nil;
aloque(p);
h <- p;
p^.chave <- 1; //ERRO

para i de 1 até 3 faça
  início
     aloque(p);
     q^.chave <- p^.chave+2;
     imprima(h^.chave,p^.chave,q^.chave);
     p <- q;
   fim;
fim-para;
imprima(h^.chave,p^.chave;q^.chave);
fim.

Code in C

#include<stdio.h>
#include<stdlib.h>


typedef struct elemento *ponteiro;

struct elemento
{
    int chave;
    ponteiro prox;
};



main()
{
ponteiro p,prim,h,q;
int i;

prim=NULL;
p=(struct ponteiro * )malloc(sizeof(ponteiro));
h=p;
p.chave=1; //ERRO

for(i=0;i<3;i++)
{
   q=(struct ponteiro *)malloc(sizeof(ponteiro));
   q.chave=p.chave+2;
   printf("%d %d %d",h.chave, p.chave, q.chave);
   p=q;
}

printf("%d %d %d",h.chave,p.chave,q.chave);


}
    
asked by anonymous 04.11.2018 / 00:41

1 answer

2

There are several things wrong, which probably arise from confusion about some concepts. Let's start with the error:

p.chave=1; //ERRO

p is a pointer, so p.chave is invalid. First you have to access the value pointed to by p and then to the field:

(*p).chave = 1;

If you want to use simple and common notation you can instead write:

p->chave = 1;

That has the same meaning.

It would be valid if you had the object declared directly something like:

struct elemento p;
p.chave = 1;

This error is propagated by all .chave that has the program out.

The declaration of p is also wrong:

p=(struct ponteiro * )malloc(sizeof(ponteiro));

This already has to do with the typedef defined before it creates an alias for a type that is a pointer and that is almost always a source of problems, for not making this detail evident, that That's what happened here. The type to allocate is struct elemento and the ponteiro defined in the typedef is not a synonym since it has precisely the * . This causes both cast to be wrong and sizeof :

p=(struct ponteiro * )malloc(sizeof(ponteiro));    
//    ^-----^--------------------------^------ ambos errados

Correct would be:

p = (struct elemento*) malloc(sizeof(struct elemento));

Or if you want to use typedef :

p= (ponteiro) malloc(sizeof(struct elemento));

This error also applies to the other malloc made below.

I advise you to understand how pointers and allocations work, and all sorts of associated concepts before you even start trying to do a conversion.

Edit :

Complete code corrected according to the errors pointed out in the response:

#include<stdio.h>
#include<stdlib.h>

typedef struct elemento *ponteiro;

struct elemento {
    int chave;
    ponteiro prox;
};



main() {
    ponteiro p,prim,h,q;
    int i;

    prim=NULL;
    p=(ponteiro)malloc(sizeof(struct elemento));
    h=p;

    //as duas versões para ver como ambas funcionam
    p->chave=1; 
    (*p).chave = 1;

    for(i=0; i<3; i++) {
        q=(ponteiro)malloc(sizeof(struct elemento));
        q->chave=p->chave+2;
        printf("%d %d %d",h->chave, p->chave, q->chave);
        p=q;
    }

    printf("%d %d %d",h->chave,p->chave,q->chave);
}

See how you compile in Ideone

Now that compiling and running is different from doing what you want the program to do, I do not even know exactly what it is.

    
04.11.2018 / 01:04