c结构体施展操作(将持续更新)

来源:互联网 发布:石家庄盛光网络怎么样 编辑:程序博客网 时间:2024/05/16 01:39

结构体是c语言中最像对象的数据结构,他有一大堆属性,但是没有属性,但是没有方法,所以你没有办法向对象发送消息。
大学数据结构上对二叉树的操作对结构体的声明看着结构体用着挺舒服的,可是实战是行不通的。
平台:visual communnity 2015
结构体声明:
片段1:编译成功

#include <stdio.h>typedef struct Bnode {    int key;    Struct Bnode* left;    Struct Bnode* right;}node;int main() {    node* a = (node*)malloc(sizeof(node));    a->key = 0;    a->left = (node*)malloc(sizeof(node));    a->right = (node*)malloc(sizeof(node));    printf("%d",a->key);    return 0;}

代码2:(编译错误)

#include <stdio.h>typedef struct Bnode {    int key;    node* left;    node* right;}node;int main() {    node* a = (node*)malloc(sizeof(node));    a->key = 0;    a->left = (node*)malloc(sizeof(node));    a->right = (node*)malloc(sizeof(node));    printf("%d",a->key);    return 0;}

在学c语言的时候,从函数头声明的时候就了解了c语言是从上到下编译的,没想到这么“从上到下”!

结构体非指针

#include <stdio.h>typedef struct Bnode {    int key;}node;int main() {    node a;//此时已经申请了结构体空间    a.key = 0;    printf("%d",a.key);    printf("%d",sizeof(node));//计算结构体的空间。    return 0;}

结构体二叉树节点

#include <stdio.h>typedef struct Bnode {    int key;    struct Bnode* left;    struct Bnode* right;}node;int main() {    node* a = (node*)malloc(sizeof(node));    a->key = 0;    a->left = a;    a->right = a;    printf("%d",a->left->key);    return 0;}

这样可以解决结构体的语法错误。

0 0
原创粉丝点击