Error referencing Struct with Pointers

Asked

Viewed 88 times

0

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

Error Code:

error: request for Member 'chave' 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);


}
  • Usually it is better to understand the problem and solve in the language you want, without trying to convert something.

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 by p and only then to the countryside:

(*p).chave = 1;

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

p->chave = 1;

Which has the same meaning.

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

struct elemento p;
p.chave = 1;

This error spreads through all .chave that has the program outside.


The statement of p is also wrong:

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

This is already about typedef defined before it creates a alias for a guy who’s a pointer and almost always a source of trouble, for not making that detail obvious, which is what happened here. The type to allocate is struct elemento and the ponteiro defined in typedef is not a synonym because it has precisely the *. This makes it so wrong cast like the 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 the typedef:

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

This error also applies to the other malloc done further down.

I advise you to understand well how the pointers and allocations work and all kinds of associated concepts before you even start trying to make a conversion.

Edit:

Complete code corrected according to errors pointed out in 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 it compiles in Ideone

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

  • @It’s because you didn’t make the exchanges properly. Already include in the answer how is the code after the corrections I indicated to be easier to understand.

  • Thanks guy saved my life now, because now I can try to finish making the list of this guy there.

  • has some indication of some material so I can learn from beginner to professional on pointers and dynamic allocation as I still have a lot of difficulty understanding these two subjects

  • @Fzero Glad you helped :). However, I suggest you follow what I have indicated and try to delve into pointers and all related matter as it not only avoids problems of this kind, but also gives you a greater understanding of the code as a whole. For material in C in general the own site wiki already has several good recommendations.

Browser other questions tagged

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