struct结构体基础知识

/*
 ============================================================================
 Name        : TestStruct.c
 Author      : lf
 Version     :
 Copyright   : Your copyright notice
 Description : struct结构体基础知识
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>

//定义全局stundent结构体
struct stundent {
	char name;
	int age;
};

int main(void) {
	testStruct1();
	return EXIT_SUCCESS;
}

/**
 * 结构体的基本使用
 */
void testStruct1() {
	//使用全局结构体
	struct stundent s;
	s.name = 'A';
	s.age = 18;
	printStructInfo(s);
	printf("name=%c,age=%d\n", s.name, s.age);
	printf("=================\n");

	//定义局部teacher结构体
	struct teacher {
		char name;
		int age;
	};
	struct teacher t;
	t.name = 'B';
	t.age = 35;
	printf("name=%c,age=%d\n", t.name, t.age);
	printf("=================\n");

	//如下亦可初始化结构体,但是可读性不强
	struct teacher te={'C',55};
	printf("name=%c,age=%d\n", te.name, te.age);
}

void printStructInfo(struct stundent s){
	printf("name=%c,age=%d\n", s.name, s.age);
}

时间: 2024-12-17 19:02:52

struct结构体基础知识的相关文章

C#基础知识---struct(结构体)

结构体和类有点类似    我们定义一个类的时候    是class   类名   定义结构体的时候是 struct   结构体名 下面我们来看看结构体的写法  以及一些注意点 struct Point { // public int x=10; 这种写法错误 public int x ; public int y; public int X { get { return x; } set { x = value; } } public void SayHi() { Console.WriteLi

go struct结构体

struct结构体 用来自定义复杂数据结构 struct里面可以包含多个字段(属性) struct类型可以定义方法,注意和函数的区分 struct类型是值类型 struct类型可以嵌套 Go语言没有class类型,只有struct类型 1.struct 声明 type 标识符 struct { field1 type field2 type } 样例: type Student struct { Name string Age int Score int } 2.struct 中字段访问:和其他

struct结构体自动化构想性能监控

TABLE_NAME不查询其他数据库中的表.外为了以防万一,可以在SQL语句中写表时加上数据库比如SELECT column_name ,TABLE_NAME,TABLE通过查询information_schema库中的tables DeviceType | tinyint(3) unsigned | YES | | NULL | | | VideoInputNum | tinyint(3) unsigned | YES | | NULL | | | VideoCodecCapacity如果结构

struct 结构体以及 typedef struct

1.C语言中,结构体的声明如下:首先关键字struct表示接下来是一个结构,后面是一个可选的标记,用来引用该结构的快速标记. struct book { char title[MAXN]; char author[MAXM]; float value; }; 上述结构声明,是"结构设计"的意思.它告诉编译器如何表示数据,而没有让计算机为数据分配空间. 然后声明或定义一个结构体变量时,如下: struct book library; 其中struct 不能省. 综合上述的两个,可以合二为

C语言中结构体基本知识

1 声明结构体 声明结构体 关键字struct 结构体名字 {类型修饰符 成员名};声明一个学生的结构体 第一种方法 struct student{    char name[20]; // 结构体成员中可以使用另外的构造类型.    int number;   // 每个成员之间使用;隔开.    int age;    float score;}; typedef struct student Student; // 将现有的类型修饰符该别名为 Student 关键词 typedef 用于改

struct结构体在c和c++中的差别

非常多次遇到这个struct的问题,今天在这里简单总结一下我的理解 一.struct在C 中的使用 1.单独使用struct定义结构体类型 struct Student { int id; int name; }stu1; struct Student stu2; stu1.id=1; stu2.id=2; 上面定义了一个结构体类型struct Student 和一个结构体类型变量stu1. 所以有两种定义结构体变量的方式: 一种是这就跟在结构体定义的后面(}之后),一种是用 struct  结

c语言 struct结构体的变量声明加冒号

有些信息在存储时,并不需要占用一个完整的字节,而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1两种状态,用一位二进位即可. 为了节省存储空间,并使处理简便,C语言又提供了一种数据结构,称为"位域"或"位段".所谓"位域"是把一个字节中的二进位划分为几个不同的区域,并说明每个区域的位数.每个域有一个域名,允许在程序中按域名进行操作.这样就可以把几个不同的对象用一个字节的二进制位域来表示. [1]定义: struct 位域结构名 { 位

Swift Struct 结构体

前言 结构体是值类型,并且只有在赋予变量或常量,或者被函数调用时才被赋值. 1.结构体的创建 结构体基本结构 struct 结构体名 { var 或 let 字段名 = 初始化值 var 或 let 字段名: 类型 } struct Student { var age = 0 } var stu = Student() /// struct 结构体关键字 /// Student 结构体名称 /// student() 创建一个结构体变量 1.1 标准定义 结构体的定义 定义结构体字段时可以直接定

(struct)结构体变量作为函数参数调用的方法小结

结构体变量.结构指针变量.结构数组作为函数的参数应用实例分析 struct stud { long int num; float score; }; /*结构体变量作为函数的参数,修改之后的成员值不能返回到主调函数*/ void funvr(struct stud t) { t.num=2000101; t.score=71.0; } /*结构体数组作为函数的参数,修改后的元素的成员值能返回到主调函数*/ void funar(struct stud t[]) //void funar(stru