Use of typedef for pointer

3

If I have already set a pointer to my structure because I can not allocate 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;
}
    
asked by anonymous 10.09.2018 / 23:06

1 answer

6

So?

#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);
}

A variable was missing, you can not declare a variable without naming it.

It would look even better like this:

#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);
}

See running on ideone . And in Coding Ground . Also I placed GitHub for future reference .

    
10.09.2018 / 23:11