1. 程式人生 > >子函式中為指標分配空間

子函式中為指標分配空間

#include <stdio.h>
#include <malloc.h>

typedef struct node{
    int data;
    struct node *next;
}Node,*Linklist;

void my_malloc(Linklist *node)
{
    if(*node == NULL){
        *node = (Linklist)malloc(sizeof(Node));
    }
}

int main(void)
{
    Linklist node = NULL;
    my_malloc(&node);

    if(NULL == node){
        printf("node is null\n");
    }else{
        node->data = 12;
        printf("%d\n",node->data);
        free(node);
    }
}