I have a code like this in C:
typedef struct _a1{
int d1;
} a1, *pa1;
I can create another pointer and use it like:
a1 *pa2 = NULL;
pa2 = (a1*)malloc(sizeof(a1));
Trying the same for "pa1" fails. How do I use pointer "pa1"?
I have a code like this in C:
typedef struct _a1{
int d1;
} a1, *pa1;
I can create another pointer and use it like:
a1 *pa2 = NULL;
pa2 = (a1*)malloc(sizeof(a1));
Trying the same for "pa1" fails. How do I use pointer "pa1"?
In case it's not clear, pa1 is not a pointer to an a1 struct. What you're doing with the typedef is just defining two types - one a type that is pointer to your struct (p1=a1*), and one the struct itself (a1). For me this works without a problem:
#include <stdio.h>
#include <stdlib.h>
typedef struct _a1{
int d1;
} a1, *p1;
int main() {
p1 p2 = NULL;
p2 = (p1)malloc(sizeof(a1));
printf("%p\n",p2);
return 0;
}
And I suggest reading Is it a good idea to typedef
pointers? — I wholeheartedly agree.