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 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;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值