数据结构学习(结构体复习)

简介: 结构体 为什么会出现结构体 为了表示一些复杂的数据,而普通的基本类型变量无法满足要求 什么叫做结构体 结构体是用户根据实际需要自己定义的复合数据类型 如何使用结构体 两种方式: struct Student st = {1000,"zhangxu",20}; struct Student *pst = &st; 1.

结构体

为什么会出现结构体

为了表示一些复杂的数据,而普通的基本类型变量无法满足要求

什么叫做结构体

结构体是用户根据实际需要自己定义的复合数据类型

如何使用结构体

两种方式:

struct Student st = {1000,"zhangxu",20};

struct Student *pst = &st;

1.

St.sid

2.

Pst->sid

Pst所指向的结构体变量中的sid这个成员

注意事项

1.结构体变量不能加减乘除,但可以相互赋值

2.普通结构体变量和结构体指针变量作为函数传参的问题

#include<stdio.h>
#include<string.h>
struct Student
{
 int sid;
 char name[200];
 int age;
};//分号不能省
int main(void)
{
 struct Student st = {1000,"zhangxu",20};
 printf("%d   %s   %d\n",st.sid,st.name,st.age);
 st.sid = 99;
 //st.name = "lisi";//error
 strcpy(st.name,"lisi");
 st.age = 22;
 printf("%d   %s   %d\n",st.sid,st.name,st.age);
 return 0;
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include<stdio.h>
struct Student
{
 int sid;
 char name[200];
 int age;
};
int main(void)
{
 struct Student st = {1000,"zhangxu",20};
 //st.sid = 99;//第一种方式
 struct Student *pst;
 pst = &st;
 pst->sid = 99;//第二种方式 pst->sid等价于(*pst).sid,而(*pst).sid等价于 st.sid,所以 pst->sid等价于st.sid
 return 0;
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#include<stdio.h>
struct Student
{
 int sid;
 char name[200];
 int age;
};
int f(struct Student * pst);
int g(struct Student * st);
int main(void)
{
 struct Student st;//和 int i一样
 int i;//内存给分配空间了
 f(&st);
 g(&st);

 return 0;
}
int f(struct Student * pst)
{
 (*pst).sid = 99;
 strcpy(pst->name,"lisi");
 pst->age = 22;
}
int g(struct Student *st)
{
 printf("%d   %s   %d\n",st->sid,st->name,st->age);
}

 

目录
相关文章
|
2月前
|
存储 Java API
JAVA零基础小白学习免费教程day13-Collection&数据结构
JAVA零基础小白学习免费教程day13-Collection&数据结构
84 0
|
1月前
|
存储 C语言
【数据结构】顺序表的学习
【数据结构】顺序表的学习
|
1月前
|
算法 Java 索引
【数据结构与算法】4、双向链表(学习 jdk 的 LinkedList 部分源码)
【数据结构与算法】4、双向链表(学习 jdk 的 LinkedList 部分源码)
31 0
|
1月前
|
存储 算法 Java
【数据结构与算法】1、学习动态数组数据结构(基本模拟实现 Java 的 ArrayList 实现增删改查)
【数据结构与算法】1、学习动态数组数据结构(基本模拟实现 Java 的 ArrayList 实现增删改查)
44 0
|
1月前
|
算法
【数据结构】复杂度学习
【数据结构】复杂度学习
|
2月前
|
存储 NoSQL 算法
学习 Redis 基础数据结构,不讲虚的。
职场中是这样使用 redis 的。
150 1
学习 Redis 基础数据结构,不讲虚的。
|
3月前
|
存储 编译器 Linux
【C语言】【数据结构】自定义类型:结构体
这是一篇对结构体的详细介绍,这篇文章对结构体声明、结构体的自引用、结构体的初始化、结构体的内存分布和对齐规则、库函数offsetof、以及进行内存对齐的原因、如何修改默认对齐数、结构体传参进行介绍和说明。
36 0
|
3月前
|
网络协议
Reactor学习,从数据结构,内存分配,概念上分析
Reactor学习,从数据结构,内存分配,概念上分析
38 0
|
3月前
|
Ubuntu Linux 编译器
用户态协议栈学习,DKDK基本用法介绍
用户态协议栈学习,DKDK基本用法介绍
63 0
|
4月前
|
存储 算法
【数据结构-零基础学习】线索二叉树(代码+图示+解析)
【数据结构-零基础学习】线索二叉树(代码+图示+解析)
44 0

热门文章

最新文章