Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the structure of C language?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how C language constructs look. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

structure

A structure is a collection of values called member variables. Each member of a structure is a variable of a different type.

Why do we need structures?

For example, when describing a student, you need to have

name

gender

age

height

to describe them together, you need different variables, you have struct types to describe them.

declaration struct tag{ member-list//member list}variable-list;//variable list

For example, a student

struct stu{ char name[20]; char sex[5]; int age; int height;};//There is no variable list here

struct stu{ char name[20]; char sex[5]; int age; int height;}s2,s3,s4;//s2,s3,s4 global variables struct stu s5;int main(){ struct stu s1;//struct variable}

specific detailed information

When declaring structs, you can declare them incompletely

struct{ char c; int a; double a;}sa;//anonymous struct variable, variable must be defined here, otherwise cannot use int main(){ return 0;}

The compiler thinks ps and &sa are two types, which is wrong.

Data structure: The structure of data stored in memory

About the list

struct node{ int date; struct node next;};int main(){ return 0;}

So you can't tell the size of a structure, you just need to store a pointer to the next structure.

struct node{ int date; struct node* next;};int main(){ return 0;}

也可以这样(重命名使用举例)

typedef struct node{ int date; struct node* next;}node;int main(){ struct node n2 = { 0 };//2者都可以使用 node n = { 0 };//尽量不对结构体使用typedef}结构体变量的定义和初始化struct point{ int x; int y;}p1; //声明类型同时定义变量p1struct point p2;//定义结构体变量p2//初始化:定义变量的同时赋初值struct point p3 = { 1,2 };struct stu //类型声明{ char name[15];//名字 int age; //年龄};struct stu s = { "zhangsan",20 };//初始化struct node{ int date; struct point p; struct node* next;}n1 = { 10,{4,5},NULL }; //结构体嵌套初始化struct node n2 = { 20,{5,6},NULL };//结构体嵌套初始化结构体大小计算

先来观察下列代码

#include int main(){ struct S1{ char c1; int i; char c2;};printf("%d\n", sizeof(struct S1));//练习2struct S2{ char c1; char c2; int i;};printf("%d\n", sizeof(struct S2));//练习3struct S3{ double d; char c; int i;};printf("%d\n", sizeof(struct S3));//练习4-结构体嵌套问题struct S4{ char c1; struct S3 s3; double d;};printf("%d\n", sizeof(struct S4));return 0;}

发现并不是数据类型大小的简单相加

存在对齐

如何计算?

首先得掌握结构体的对齐规则:

1. 第一个成员在与结构体变量偏移量为0的地址处。

2. 其他成员变量要对齐到某个数字(对齐数)的整数倍的地址处。

对齐数 = 编译器默认的一个对齐数 与 该成员大小的较小值。

VS中默认的值为8

3. 结构体总大小为最大对齐数(每个成员变量都有一个对齐数)的整数倍。

4. 如果嵌套了结构体的情况,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整

体大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍。

为什么存在内存对齐 ?

大部分的参考资料都是如是说的:

1. 平台原因(移植原因):

不是所有的硬件平台都能访问任意地址上的任意数据的;某些硬件平台只能在某些地址处取某些特

定类型的数据,否则抛出硬件异常。

感谢各位的阅读!关于"C语言结构体是怎么样的"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report