Pointers and data type hidden in C

0

EDIT: I'll leave the codes just below the same text.

Good evening. Please see the attached image.

It is the implementation of a function whose prototype is defined in another file named "stack.h". As it is, it works perfectly.

However, if I delete line 11 and modify line 5 to typedef struct {....} stack; I have a compilation error:

  

stack.c: In function 'CreatePath':

     

stack.c: 15: 6: error: dereferencing pointer to incomplete type 'struct   stack '

 p->v = malloc(tamanho * sizeof(char));
  ^~

Could anyone help me to understand this error?

Thanks in advance!

Archive"stack.c"

--------------- stack.h -----------------

typedef struct pilha *Pilha;

Pilha criaPilha(int tamanho);

int pilhaCheia(Pilha p);

int pilhaVazia(Pilha p);

void empilha(Pilha p, char elemento);

char desempilha(Pilha p);

int topo(Pilha p);

char eleTopo(Pilha p);

----------- stack.c -----------

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

struct pilha {
    char *v;
    int topo;
    int tamanho;
};

typedef struct pilha pilha;

Pilha criaPilha(int tamanho) {
    Pilha p = malloc(sizeof(pilha));
    p->v = malloc(tamanho * sizeof(char));
    p->topo = 0;
    p->tamanho = tamanho;

    return p;
}

int pilhaCheia(Pilha p) {
    if (p->topo > p->tamanho)
        return 1;
    return 0;
}

int pilhaVazia(Pilha p) {
    if (p->topo == 0)
        return 1;
    return 0;
}

void empilha(Pilha p, char elemento) {
    if (!pilhaCheia(p)) {
        p->topo++;
        p->v[p->topo] = elemento;
    }
    else
        printf("Pilha cheia!\n");
}

char desempilha(Pilha p) {
    if (!pilhaVazia(p)) {
        p->topo--;
        return p->v[p->topo + 1];
    }
    else
        return '
 p->v = malloc(tamanho * sizeof(char));
  ^~
'; } int topo(Pilha p) { return p->topo; } char eleTopo(Pilha p) { return p->v[p->topo]; }
    
asked by anonymous 17.08.2018 / 06:58

1 answer

1
typedef struct Pilha {
    char *v;
    int topo;
    int tamanho;
} Pilha;

Pilha *criaPilha(int tamanho) {
    Pilha *p = (Pilha *) malloc(sizeof(Pilha));
    p->v = (char *) malloc(tamanho);
    p->topo = 0;
    p->tamanho = tamanho;
    return p;
}

There are a few things to note here:

  • You should not confuse pilha with Pilha . Remember that C is case-sensitive. Having Pilha and pilha as different things is to ask to do lame.

  • The malloc function always returns a pointer. Therefore, do not forget the * on the pointer.

  • Notice the strange syntax of typedef . It says that the nickname given to struct Pilha {...} is Pilha .

  • Placing casts in malloc is a good idea. The reason for this is that there are some interoperability issues between C and C ++ if you do not.

  • The C language pattern says sizeof(char) is always 1, no matter what platform it is. So you do not need to multiply by sizeof(char) within malloc .

  • Hiding a data type is a pointer with a typedef is another way of asking to do lame.

  • In the header file pilha.h , you would only use typedef struct Pilha Pilha; , while putting struct complete in pilha.c .

  • 17.08.2018 / 07:19