很简单,定义一个指针。它的类型和struct相关。如果typedef后面有多个定义,我们可以逐个拆开。
typedef int *PInt, Int, **PPInt;
我们可以拆成:
// typedef int *PInt;
// typedef int **PPInt;
// typedef int Int;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test_typedef_p();
void test_typedef_normal();
int main(int argc, char *argv[])
{
test_typedef_p();
test_typedef_normal();
return 0;
}
void test_typedef_normal(){
typedef int *PInt, Int, **PPInt;
// typedef int *PInt;
// typedef int **PPInt;
// typedef int Int;
Int a=1;
PInt pint_a = &a;
PPInt ppint_a=&pint_a;
printf("ppinta:= %d \n", **ppint_a);
}
void test_typedef_p(){
#if 0
struct node {
int data;
struct node *next;
} *head =NULL;
#else
typedef struct node {
int data;
struct node *next;
} *NodePtr, Node, *NodePer2;
Node *head =NULL;
#endif
head =(struct node*)malloc(sizeof(struct node));
memset(head, 0, sizeof(struct node));
head->data=1;
head->next=NULL;
printf(" head data is: %d \n ", head->data); NodePtr pointer_str;
pointer_str =(struct node*)malloc(sizeof(struct node));
memset(pointer_str, 0, sizeof(struct node));
pointer_str->data=1;
pointer_str->next=NULL;
printf("pointer_str data is: %d \n ", pointer_str->data);
NodePtr pointer_str2;
pointer_str2 =(Node*)malloc(sizeof(Node));
memset(pointer_str2, 0, sizeof(Node));
pointer_str2->data=1;
pointer_str2->next=NULL;
printf("pointer_str2 data is: %d \n ", pointer_str2->data);
// const NodePtr p3;
// p3 = pointer_str2;
const NodePtr p3 = pointer_str2;
//p3 =(Node*)malloc(sizeof(Node));
memset(p3, 0, sizeof(Node));
p3->data=2;
p3->next=NULL;
printf("p3 data is: %d \n ", p3->data);
}
typedef 后还可以跟数组:
typedef int IntArramy[10];
IntArray array;