结构体统一说明

前言:本人写数据结构从来不使用指针,所以关于指针的写法要重新学一下

关于结构体指针->

复制代码
//difference between structs
#include<bits/stdc++.h>
using namespace std;
struct Node{
    int h;    
}P,*p,*p_;
int main(){
    p=&P;
    p->h=5;
    cout<<p->h<<'\n';
    cout<<(*p).h<<'\n';
    //////////////////////////////
    p_=(Node *)malloc(sizeof(Node));
    p_->h=5;
    cout<<p_->h;
    //////////////////////////////
}
复制代码

object->member和(* object).member等价

可以使用(struct_name *)malloc(sizeof(struct_name))来直接重新生成一个新的结构体空间

 

 

关于struct的一些用法

复制代码
//difference between structs
//most commmon
#include<bits/stdc++.h> using namespace std; struct Node{ int h; }; //define a struct named Node, and can be followed by some objects //for example int main(){ Node a; a.h=5666; cout<<a.h; return 0; }
复制代码

typedef

//typedef makes a nickname for a type
#include<bits/stdc++.h>
using namespace std;
typedef int zyh; 
int main(){
    zyh a=114514;
    cout<<a;
} 

typedef struct

复制代码
//typedef makes a nickname for a type
#include<bits/stdc++.h>
using namespace std;
typedef struct Node{
    int h;
}G,P,*zyh;
int main(){
    Node a;
    G b;
    P c;
    a.h=1;
    b.h=2;
    c.h=3;
    cout<<a.h<<' '<<b.h<<' '<<c.h<<'\n';
    //P.h=5;  the following two codes are wrong since the P is not an object
    //cout<<P.h;
    G *zyh=&a;
    zyh->h=4;
    cout<<zyh->h;
} 
复制代码

自己调用自己

复制代码
//typedef makes a nickname for a type
#include<bits/stdc++.h>
using namespace std;
typedef struct Node{
    int h;
    Node *next;
};
int main(){
    Node a,b;
    b.h=5;
    Node* zyh=&a;
    zyh->next=&b;
    cout<<zyh->next->h;
} 
复制代码

over

posted @   saionjisekai  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
欢迎阅读『结构体统一说明』
点击右上角即可分享
微信分享提示