C结构体小结

C#上位机实战:Modbus RTU串口通信(CRC校验+超时重发完整实现) 在工业自动化上位机开发中,Modbus RTU是串口通信的事实标准协议,SerialPort是 .NET 平台操作串口的原生类,无需依赖第三方库即可实现基础通信。实际项目中,工控现场存在电磁干扰、串口丢包、设备响应延迟等问题,CRC 数据校验用于保证报文完整性,超时重发机制用于提升通信稳定性。本文从零封装一套可直接落地的 Modbus RTU 通信组件,包含完整的校验算法、超时控制、重发逻辑,并配套 WinForm 演示界面。本文基于 C# 原生SerialPort类,实现了一套生产可用。 阅读详情

结构体

1.声明一个结构体
             struct  自定义结构体类型名     结构体变量名
             同时必须声明结构体内部的所有成员类型
例如声明一个学生的成绩单           
  struct   Student    Score
   {
          char  aName[128];
          int      iAge;
          int      iNum;
          int      iScore;
   };

这是一个结构体的声明,其中包括了姓名,年龄,学号,分数。使用这种方法可以在主函数中声明自己需要的结构体

例如:struct   Student    Score; 当然可以声明结构体数组例如struct  Student Score[3];
进一步的我们可以使用typedef关键字使得我们声明起来更方便         
typedef    struct  student
{
           
       char  aName[128];
       int      iAge;
       int      iNum;
       int      iScore;
}STU;//建议这里大写,不知道为什么如果用小写总是容易出错//
 
这样声明后就可以方便的使用STU来声明了。
例如 
         STU  Score[3];和用struct 声明是一样的。
结构体的大小:
         结构体的大小是声明结构体内所有成员大小的总和
         例如上边的结构体(X86_64系统)
          sizeof(Score)=128+4+4+4=140字节
初始化及访问结构体内部的元素:
         初始化使用"."运算符
         例如初始化一个成绩: Score.iScore=95;
         特别的对于这种名字为字符串的赋值时,建议使用字符串复制函数strcpy()函数来初始化。
         strcpy(Score.iScore,"zhang san");
下面是一个完整的程序:
         使用结构体记录三个学生的成绩,姓名,年龄,学号,返回成绩最大的成绩和学号。
#include<stdio.h>
#include<string.h>
#define  SUCCESS  1
#define  FAILL  (-1)
#define  std    STD
typedef struct Student
{
    int Scoure;
    int iNumber;
    char cName[128];
    int iAge;
} std;/*使用typedef后自定义名称建议大写*/ 


int find_high_score(std *ptr,const unsigned int iLen,int *Max,int *MaxN);

int main()
{
    std student[3];
    int Max;
    int MaxN;
    int iRet;
    memset(student,0,sizeof(student));

    student[0].Scoure =80;
    student[0].iNumber =23;
    student[0].iAge =17;
    strcpy(student[0].cName ,"zhang san");
    
    student[1].Scoure =92;
    student[1].iNumber =24;
    student[1].iAge =17;
    strcpy(student[0].cName ,"wang er");
    
    student[2].Scoure =85;
    student[2].iNumber =25;
    student[2].iAge =17;
    strcpy(student[0].cName ,"li si");

    iRet = find_high_score(student,sizeof(student)/sizeof(std), &Max, &MaxN);
    if(iRet!=SUCCESS) 
    {
        return iRet;
    }
        printf("The hinghest score is %d\n",Max);
        printf("The student Number is %d\n",MaxN);
    return SUCCESS;
}




int find_high_score(std *ptr,const unsigned int iLen,int *Max,int *MaxN)/*传递进参数后使用ptr*/ 
{
    int iMaxs;
    int iMaxn;
    int i;

    if(NULL== ptr || NULL==Max || NULL==MaxN)/*注意==与=的使用*/ 
    {
        return FAILL;
    }
    if(0==iLen)
    {
        return FAILL;
    }
        iMaxs=ptr->Scoure;
        iMaxn=ptr->iNumber;
    for(i=0;i<iLen;i++)
    {
        if(iMaxs<ptr->Scoure)
        {
            iMaxs=ptr->Scoure;
            iMaxn=ptr->iNumber;
        }
        ptr++;
    }
    *Max=iMaxs;/*不能改变传递进来的指针的值,可以改变指针指向的值*/ 
    *MaxN=iMaxn;
    return SUCCESS;
}


 
 
 
35、使用 Geo 创建 GitLab 的分布式只读副本 本文详细介绍了如何使用GitLab Geo功能创建分布式只读副本,涵盖数据库主从配置、SSH密钥查找方式更改、通过Web UI添加从节点、激活哈希存储及状态检查等关键步骤。同时提供了常见问题解答和配置流程图,帮助用户快速部署并维护Geo集群,实现跨地域的高效数据同步与高可用架构。 阅读详情

相关推荐

浅谈专项测试之弱网络测试

浅谈专项测试之弱网络测试 一.弱网络测试背景 移动端产品的使用并非完全都是在流畅的wifi环境,大部分用户主要使用4G,3G,2G等网络,另外因为移动端产品使用的场景多变,如进公交,上地铁,坐电梯,使得弱网测试显得尤为重要。考虑到各种场景的客户端展示及容错,提升产品的印象和用户体验,提高产品的健壮性,弱网测试必不可少。 二.弱网络测试建议 网下需要进行功能点测试,无网络测试,网络切换测试等,测试过程除了关注功能点的实现,还应关注用户体验等各个方面 三.弱网络测试通用功能点总结 四.弱网络测试用例实例

ITstudy001的博客 1011

C语言结构体小结

关于结构体的声明: struct Student { char name[20]; char sex; int age; char addr[40]; }; //然后定义一个Student 类型的 student变量 struct Student student; struct Man { int age; //这样就好了 int score; }; int main() { struct Man man1 = {30,80}; } 定义结构体的时

qq_45131570的博客 313

DaVinci Developer中枚举类型变量的创建与实战应用

本文详细介绍了在DaVinci Developer中创建和使用枚举类型变量的方法与实战应用。通过智能温控系统的案例,阐述了枚举类型在嵌入式开发中的核心价值,包括提升代码可读性、类型安全性和维护便捷性。文章还提供了枚举类型在状态机实现和多模块协同中的高级应用技巧,以及调试与优化的实用建议。

ik678901234的博客 513

C 结构体小结

看了三天结构体,是时候总结一下了。   关于结构体的声明:    struct Student{ char name[20]; char sex; int age;

wangchaoqi1985的博客 139

DolphinScheduler工作流中ChunJun任务保存失败的深度解析与实战修复

本文深度解析了在DolphinScheduler工作流中创建ChunJun任务时遭遇“parameter invalid”保存失败的普遍问题。其根源在于前端表单的`customConfig`参数默认值与后端校验逻辑不匹配。文章提供了两种实战修复方案:一是修改前端源码并重新编译,二是热更新已部署的JS文件,并对比了其适用场景与风险,帮助用户彻底解决此集成障碍。

weixin_29010003的博客 399

山景BP10_128DBG开发板ADC_Key驱动

最近在学习山景BP10_128DBG开发板,想使用开发板上的按键。看了看原理图和之前用过的按键好像有点不一样。常见的按键都是判断IO口是否被拉低或拉高来判断按键是否被按下。山景BP10_128DBG开发板上的按键是ADC按键,每个按键按下之后ADC的采样值是不同的,通过ADC采样回来的值进而判断哪个按键按下。这种方式核心思想就是用一个ADC通道来节约IO口,对于IO紧张的芯片是一个不错的解决思路。

weixin_56646002的博客 1312

【C语言】结构体小结

【C语言】结构体小结

weixin_73185295的博客 334

自然语言处理之话题建模:BERTopic模型详解

BERTopic是一种基于BERT的先进话题建模技术,它结合了BERT的语义理解能力和非参数聚类算法HDBSCAN的灵活性,能够生成高质量、语义丰富的话题。与传统的LDA(Latent Dirichlet Allocation)话题模型相比,BERTopic能够更好地捕捉文本的复杂语义结构,因为它利用了预训练的BERT模型来嵌入文本,从而在话题建模中引入了深度学习的力量。聚类是无监督学习的一种,其目标是将数据集中的样本按照相似性进行分组,每个组称为一个“簇”。

zhubeibei168的博客 4545

c语言小结——结构体

一、声明结构的形式 1.标准格式: struct message { char name[size]; double money; }; 2.typedef的妙用:参见利用Typedef简化结构体的使用 二、结构体的初始化 1.初始化格式: 1.struct message bank={jiang,36}; 2.struct message bank={.name="jiang"...

Tristan的博客 344

桌面运维工程师面试题详解(一)

桌面运维工程师面试题详解(一)

jks212454的博客 1428

1、世界级模拟电路设计指南

本文详细探讨了模拟电路设计的各个方面,从基础概念到实际应用,涵盖了元件选择、滤波器设计、反馈系统、噪声问题以及SPICE仿真的优缺点。文中还提供了设计实例和无计算机设计方法,旨在帮助工程师提升模拟电路设计的效率与可靠性。

s1t2u3的博客 99

C语言小结之结构体

一、什么是结构体? 结构体也叫结构,是由一系列具有相同类型或不同类型的数据构成的数据集合。 二、结构体的声明 struct tag { member_list; }variable_list; //比如描述一个学生 struct Stu { char name[20];//姓名 int age;//年龄 char sex[3];//性别 ...

Chenxi13 348

R语言做一个 GM (1,1)灰色预测模型

累加生成是GM(1,1)模型的另一个重要步骤,它通过构造累加生成序列,把非线性非平稳时间序列转化为线性平稳时间序列。GM(1,1)是一种基于灰色系统理论的预测模型,通常用于中小样本的非线性、非平稳时间序列数据分析。灰色预测模型的核心是灰度处理,将原始时间序列转化为灰色时间序列。这里使用R语言中的diff函数对时间序列进行一次差分,然后对结果取累加和得到灰度序列。以上是GM(1,1)灰色预测模型在R语言中的实现步骤。需要注意的是,GM(1,1)模型适用于中小样本的时间序列预测,对于长期预测效果可能较差。

Mrrunsen的博客 3842

C语言小结--结构体

1 什么是结构体 结构体是一种聚合数据类型,它是一种数据元素的聚合。比如我们描述一个学生时,要求有他的姓名、学校、分数等等信息,在之前的处理中,我们通常会使用单独的数据类型来描述。比如,定义一个字符串数组来描述姓名char name[20] = &amp;quot;xiaoming&amp;quot;,定义一个浮点数来描述成绩float f = 96.5等等。但是这种做法有个缺点。所有的这些数据都是为了描述一个学生来定义的,但是它...

静静流淌的柯溪的博客 7800

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

c/c++结构体对齐小结

因为我看C++对象模型的时候,遇到了几个内存布局都是有关于对齐的一些细节,故此对结构体对齐再做一份小结,有人说:结构体对齐这个东西是依赖于编译器的,因此不用去研究,真的嘛? 也许是,也许不是,要看你是做那个行业的了,如果你是做系统地层,网络通讯,嵌入式系统的,一个字节的节省,也许对你是很大的期望呢。虽然具体的对齐方式是因编译器而异,但是对齐的基本原理是不变的,那个原理也许能指导我们编写程序

jiangnanyouzi的专栏 1万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C语言结构体指针数组小结

第一次写博客,有点小激动。话不多说,进入正题。 c语言最常见的几种东西:结构体,数组,指针和函数。单个定义很好理解,组合在一起就会让人头大。先说函数、数组和指针的组合。 1、函数、数组和指针的组合 包括指针函数、函数指针、指针数组和数组指针,先从根本上介绍这几者的区别。 为了直观理解,下面具体介绍: 1.1 数组指针 ...

qq_22393361的博客 4825

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【C语言】结构体定义 typedef struct 用法详解和用法小结

结构体定义 typedef struct 用法详解和用法小结 文章目录结构体定义 typedef struct 用法详解和用法小结0. 前言1. 首先:在C中定义一个结构体类型要用typedef:2. 其次:在c++中如果用typedef的话,又会造成区别:3. 掌握上面两条就可以了,不过最后我们探讨个没多大关系的问题4. C/C++中typedef struct和struct的用法5. 问答6. 用法小结 0. 前言 typedef是类型定义的意思。 typedef struct 是为了使用这个结构体方

weixin_44948467的博客 4万+

C语言结构体对齐小结

struct A {      int      a;      char    b;      short c; };  开辟内存8字节 struct B {      char    b;       int      a;       short c; }; 开辟内存12字节 struct C {      char    b;       int

Andy的专栏 490

c语言之结构体知识小结

结构体知识粗练 结构是一种构造数据类型,且各成员类型可以不同。 (1) 学生信息结构定义: struct student { int num; char name [10]; int computer,math,english; double average; }; 在主函数中定义结构变量:struct student s1,math; 结构变量可以通过结构成员操作符“.”对其某个成员进行引用。(s1.average)若结构变量类型相同,可以直接赋值:max=s1. 结...

weixin_48022862的博客 490

C语言 struct(结构体)小结

C语言结构体 今天有个同学请叫我,C语言 结构体 的内容。也是好久没碰了,算给自己复习了,也希望可以帮助大家。下面我们 言归正传。 基本结构 // {} 后的 ; 也是基本结构中的,不能省略 struct student { // 属性定义。。。 }; 创建 结构体 时候 ,同时定义变量 // 定义了 wang 和 students[] 两个 变量 struct students { char *name; int number; } wang, students[3];

manba 795

C语言:结构体题小结

文章目录找出总分最高的学生通讯录排序查找书籍 找出总分最高的学生 给定N个学生的基本信息,包括学号(由5个数字组成的字符串)、姓名(长度小于10的不包含空白字符的非空字符串)和3门课程的成绩([0,100]区间内的整数),要求输出总分最高学生的姓名、学号和总分。输入格式:输入在一行中给出正整数N(≤10)。随后N行,每行给出一位学生的信息,格式为“学号 姓名 成绩1 成绩2 成绩3”,中间以空格分...

ABlackDay的博客 1704
上一篇: 计算机网络基础1
下一篇: void 与泛型计算
zmrlinux
博客等级 码龄12年 409粉丝 327原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值