Pointer struct

Asked

Viewed 141 times

1

With a view to the following structures:

typedef struct celEstado *apontadorEstado;

typedef struct{

    char nome[30];
    int populacao;
    int beneficiarios;
    int qtdCidades;
    float idh;
    apontadorEstado proxEstado;
    apontadorCidade Arv;

}celEstado;

I’d like to ask you a question about apontadorEstado.

When I wrote the structure, I had in mind that I was creating an pointer struct that received the address of a state cell, but now I’m in doubt, because whenever I use a variable of the type apontadorEstado the compiler issues Warning because, according to him, I’m using a kind of pointer incompatible with the type struct celEstado. On the other hand, I have to put a pointer to the next state of the list, and I can’t declare the same in the form of celEstado *proxEstado because the compiler won’t allow it, so the way I found to do that was with the apontadorEstado

Exemplifying the error:

int main(){

    celEstado newEstado; //instância da _struct_ celEstado
    apontadorEstado aux; //Auxiliar do tipo apontadorEstado(Ponteiro)

    strcpy(newEstado.nome, "Bahia"); //inserindo o nome do estado
    aux = newEstado.proxEstado; // aux recebendo o endereço do próximo estado
    aux = (celEstado*)malloc(sizeof(celEstado));//alocando a memória pra uma célula estado
    strcpy(aux->nome, "Amazon");//o erro está aqui

    printf("%c", newEstado.nome[0]);//imprimindo o primeiro caractere
    printf("%c", aux->nome[0]);

    return 0;
}

Printed error:

main. c:25:15: error: Dereferencing Pointer to incomplete type 'pointing strcpy(aux->name, "Amazon");

Warning pointer:

main. c:24:9: Warning: assignment from incompatible Pointer type [-Wincompatible->Pointer-types] aux = (celEstado*)malloc(sizeof(celEstado));

Objectively:

  • I’m wrong about the meaning of apontadorEstado?
  • If not, why compiler error?
  • How to declare a pointer to the next state correctly?

2 answers

3


I’m wrong about the meaning of the point?

Yes, but I can’t say exactly what because it’s not quite clear in the question. Let’s understand that what you’re creating is a pointer to struct and not the other way around. And the variable name uses hungarian notation that is not usually recommended.

If not, because the compiler has an error?

But it is, and I’m not sure why the compiler is in error since most of what’s in the larger paragraph I can’t answer because I don’t know how it was used, there’s no context, there’s no error, there’s no code showing it.

How to declare a pointer to the next state correctly?

It depends on what you want to do, but in my code there was no mistake, so the way to do this is how you did it.

With editing it seems you want to do this:

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

typedef struct Estado CelEstado;

typedef struct estado {
    char nome[30];
    int populacao;
    int beneficiarios;
    int qtdCidades;
    float idh;
    CelEstado *proxEstado;
} Estado;

int main() {
    Estado newEstado; //instância da _struct_ celEstado
    strcpy(newEstado.nome, "Bahia"); //inserindo o nome do estado
    Estado *aux = malloc(sizeof(Estado));//alocando a memória pra uma célula estado
    strcpy(aux->nome, "Amazon");//o erro está aqui
    printf("%c", newEstado.nome[0]);//imprimindo o primeiro caractere
    printf("%c", aux->nome[0]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I will wait to confirm to complete, but note that I have simplified taking unnecessary codes.

You need to have an incomplete type declared before to be able to use within the structure, but it should not be a pointer because you still don’t have all the necessary information, the pointer could only be created after the already defined structure has everything it needs. And then you obviously couldn’t use it inside struct because it has not yet been defined at this time.

  • Sorry for the lack of code, I edited the question with the applied code and with the printed error

  • Yes, that’s what I want, because I need to make a chained list of Brazilian states, each with a pointer to a tree of cities that belong to it. There I am breaking my head with the function that will read the files with the information and later store them in the cells.

3

UPDATING

The error is on this line:

typedef struct celEstado *apontadorEstado;

and the cause of the error is the fact that "celEstado" in your program is not a struct, but rather a typedef for an unnamed struct that you declared so

typedef struct {
  ...
} celEstado;

so you can’t use "typedef struct celState *appointed state;" as you did.

One way to fix the build error without touching the program too much is to write like this:

typedef struct CelEstado *apontadorEstado; // <--- CelEstado C maiusculo
typedef struct CelEstado  // incluido o nome da estruta em maiusculas
{
  ...
} celEstado;

Writing the way I showed in my example this problem does not happen.

PS. C posts that I see here usually do this, declare a typedef for a structure pointer. I think it’s bad, I think it’s less readable than using the pointer explicitly, just like I did in my example.

ORIGINAL POST BELOW

Your question is not very clear, but I would change the structure statement to look like below, I think it would be easier to understand, and you would not have compilation problems:

typedef struct celEstado CelEstado;
typedef struct celCidade CelCidade;

struct celEstado
{
  char nome[30];
  int populacao;
  int beneficiarios;
  int qtdCidades;
  float idh;

  // apontadorEstado proxEstado;
  CelEstado* pProxEstado;

  // apontadorCidade Arv;
  CelCidade* pCidade;

};           

Note that the part referring to "pointing Age" I put on my own, to look like "pointing".

PS. what you call "pointer struct" is actually "pointer to struct", or "pointer to structure".

  • The problem is not exactly of compilation, sorry for not being very clear, I will reread the question and edit to improve. The problem is when I have apply this pointer to apontadorEstado, is emitting data type incompatibility error

  • the error is definitely compilation...the messages that you have put in your question are compilation error messages

Browser other questions tagged

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