结构体统一说明

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

关于结构体指针->

//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 @ 2022-04-15 14:56  saionjisekai  阅读(42)  评论(0编辑  收藏  举报