本文主要是介绍typedef,后加指针,数组等,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
很简单,定义一个指针。它的类型和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 0struct node {int data;struct node *next;} *head =NULL;
#elsetypedef struct node {int data;struct node *next;} *NodePtr, Node, *NodePer2;Node *head =NULL;
#endifhead =(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;
这篇关于typedef,后加指针,数组等的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!