Use of typedef for pointer

Asked

Viewed 135 times

3

If I’ve already set a pointer to my structure why can’t I make the allocation of it.

#include <stdio.h>
#include <stdlib.h>
struct ponto
{
   int a, b;
};

typedef struct ponto *Ponteiro; // define um ponteiro para estrutura ponto 
typedef struct ponto estrutura; // aqui chamo a estrutura ponto de estrutura

int main(int argc, char** argv)
{ 
    Ponteiro  = malloc(sizeof(estrutura)); 

    return 0;
}

1 answer

6


Thus?

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

struct ponto {
   int a, b;
};

typedef struct ponto * Ponteiro;
typedef struct ponto estrutura;

int main() { 
    Ponteiro p = malloc(sizeof(estrutura));
    printf("%p", (void *)p);
}

Missing a variable, you cannot declare a variable without naming it.

It would look even better this way:

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

typedef struct ponto {
   int a, b;
} Estrutura;

typedef Estrutura* Ponteiro;

int main() { 
    Ponteiro p = malloc(sizeof(Estrutura));
    printf("%p", (void *)p);
}

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

Browser other questions tagged

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