C语言或C++结构体及其变量的创建方式汇总

编程入门 行业动态 更新时间:2024-10-28 01:18:23

C语言或C++结构体及其<a href=https://www.elefans.com/category/jswz/34/1771380.html style=变量的创建方式汇总"/>

C语言或C++结构体及其变量的创建方式汇总

第一种:

struct 结构体类型名{

任意类型 变量;

};

结构体类型名  *结构体变量=new 结构体类型名; 

#include<bits/stdc++.h>
using namespace std;
struct LNode {int data;struct LNode *next;
};
/*void PutElem(LNode *t){while(t->next!=NULL){cout<<t->data<<endl;t=t->next;}cout<<t->data<<endl;
}*/
//输出链表的元素
int main() {LNode *L=new LNode;L->next=NULL;
//输入链表/*for(int i=0; i<3; ++i) {LNode *p=new LNode;cin>>p->data;p->next=L->next;L->next=p;}*///PutElem(L->next);return 0;
}

第二种 

struct 结构体类型名{

};

结构体类型名  *结构体变量=(结构体类型名*)malloc(sizeof(struct  结构体类型名)); 

#include<bits/stdc++.h>
using namespace std;
struct LNode {int data;struct LNode *next;
};
int main() {LNode *L=(LNode*)malloc(sizeof(struct LNode));L->next=NULL;return 0;
}

 第三种

typedef struct 结构体类型名{

任意类型 变量;

};

结构体类型名*  结构体变量=new 结构体类型名; 

#include<bits/stdc++.h>
using namespace std;
typedef struct LNode {int data;struct LNode *next;
};
int main() {LNode* L=new LNode;L->next=NULL;return 0;
}

 

更多推荐

C语言或C++结构体及其变量的创建方式汇总

本文发布于:2023-11-17 00:35:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634960.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   语言   结构   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!