Stack algorithm problem in C (Struct and function do not "recognize")

Asked

Viewed 51 times

0

Hello, I’m starting in the study of data structures and one of the algorithms passed is for data increment in a vector as in a stack (ie, the last incremented is the first to come out) I am in the incrementation part and I am having problems with the performance of my pointer when pointing out the attributes of my stack struct. I have tried to do this without it being a pointer, because then the code seems to "recognize" the attributes, but it has not worked. For now the code is like this.

#include <stdio.h>
#include <stdlib.h>
#define stack_length 100

struct stack {

int top;
int itens[stack_length];
};
void push(struct stack *p, int e ){
if (p.top == (stack_length - 1)){
    printf ("\nestouro de pilha");
    exit(1);
}
    p.itens[++(p.top)] = e;
    printf("%d",e);

void main (){
int e = 6;

}

As for the mistakes they are: Error request for member 'top' in something not a structure or a union Error request for member 'itens' in something not a structure or a union

In rows 11 and 16 respectively:

Line 11: if (p.top == (stack_length - 1)){ (...)

Line 16: p.itens[++(p.top)] = e;

ps: I have not yet properly touched the main function, I filled there only for consciousness awakening...

  • 2

    As p is a pointer to the structure use: p->top and p->itens... or, if you prefer to use the rating with '.': (*p).top.

1 answer

0

I don’t know which compiler you’re using but in codeblocks when we work with pointers "->" is used instead of "." , another thing when you are modifying a pointer it must be between parentheses and with asterisks before type: (*p)->item[++((*p)->top)]

Browser other questions tagged

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