zoukankan      html  css  js  c++  java
  • 创建结点 与 分配内存 Function to create a Node. Allocates memory for a new node. 主动申请内存 链表 指针的写法

    Self Referential Data Structure in C - create a singly linked list http://www.how2lab.com/programming/c/link-list1.php

    #include <stdio.h>
    typedef struct st  {
    	int  data;
    	struct st* s;
    } alias;
    alias *createNode() {
    	alias *new;
    	new=(alias *)malloc(sizeof(alias));
    	return new;
    }
    int main() {
    	alias a,*b;
    	b=createNode();
    	a.data=123;
    	b->data=456;
    	a.s=b;
    	printf("%d,",a.data);
    	printf("%d,",a.s->data);
    	return 1;
    }
    

      

    注意:

    typedef struct st {
    int data;
    struct st *s;
    } alias;

    typedef struct st {
    int data;
    struct st* s;
    } alias;

     同

    alias* createNode() {
    alias *new;
    new=(alias *)malloc(sizeof(alias));
    return new;
    }

    alias *createNode() {
    alias *new;
    new=(alias *)malloc(sizeof(alias));
    return new;
    }

     同

    #include <stdio.h>
    typedef struct st  {
    	int  data;
    	struct st* s;
    } alias;
    alias *createNode() {
    	alias *new;
    	new=(alias *)malloc(sizeof(alias));
    	return new;
    }
    int main() {
    	alias a,*b;
    	b=createNode();
    	a.data=123;
    	b->data=456;
    	a.s=b;
    	printf("%d,",a.data);
    	printf("%d,",a.s->data);
    	alias *c;
    	c=createNode();
    	c->data=789;
    	(*b).s=c;
    	printf("%d,",a.s->s->data);
    	return 1;
    }
    

      

    #include <stdio.h>
    int* createInt() {
    	int *new;
    	new=(int *)malloc(sizeof(int));
    	return new;
    }
    int main() {
    	int *a;
    	int *b;
    	a=createInt();
    	b=createInt();
    	*a=123;
    	*b=456;
    	printf("%d,",*a);
    	printf("%d,",*b);
    	return 1;
    }
    

      

    #include <stdio.h>
    int* createInt() {
    	int *new;
    	new=(int *)malloc(sizeof(int));
    	return new;
    }
    int main() {
    	int *a;
    	int* b;
    	a=createInt();
    	b=createInt();
    	*a=12;
    	*b=34;
    	printf("%d,",*a);
    	printf("%d,",*b);
    	return 1;
    }
    

      

    对指针变量的赋值

    以下未报错

    #include <stdio.h>
    int* createInt() {
    	int *new;
    	new=(int *)malloc(sizeof(int));
    	return new;
    }
    int main() {
    	int *a;
    	/*
    	int* b;
    
    	a=createInt();
    	b=createInt();
    	*/
    	a=12;
    	/*
    	*b=34;
    	printf("%d,",*a);
    	printf("%d,",*b);
    	*/
    	printf("CAN!,");
    	return 1;
    }
    

      

    15 3 D:editorToolmain.c [Warning] assignment makes pointer from integer without a cast

    4 13 D:editorToolmain.c [Warning] incompatible implicit declaration of built-in function 'malloc'

  • 相关阅读:
    流程控制引擎组件化
    (七):C++分布式实时应用框架 2.0
    (六):大型项目容器化改造
    (五):C++分布式实时应用框架——微服务架构的演进
    (四):C++分布式实时应用框架——状态中心模块
    (三):C++分布式实时应用框架——系统管理模块
    (二): 基于ZeroMQ的实时通讯平台
    (一):C++分布式实时应用框架----整体介绍
    分布式压测系列之Jmeter4.0第一季
    选择 NoSQL 需要考虑的 10 个问题
  • 原文地址:https://www.cnblogs.com/rsapaper/p/10542989.html
Copyright © 2011-2022 走看看