数组,指针,结构体的使用
来源:互联网 发布:推淘宝店铺的微博 编辑:程序博客网 时间:2024/06/13 16:22
在使用C语言表述一组有特定格式,顺序的数据时,如报文。报文数据,经常会包含不同功能的数据,为了区分顺序和定位数据在报文中的位置,通常我们会采用定义结构体的方式来表示报文,如接收。如果想发送数据时,我们只需要顺序发送,不关心具体位置,此时定义数组较为方便。为了兼顾这两种情况,可以通过合理的使用数组,指针,和结构之间的转换来解决该问题。
#include <stdio.h>unsigned char a[2] = {0,1};typedef struct{unsigned char a[10];unsigned char b;unsigned char c;}str_TypeDef;str_TypeDef str;unsigned char *p = (unsigned char *)&str;unsigned char d[12]; str_TypeDef *pstr = (str_TypeDef *)&d;void test(void){unsigned char *p = &a;//*(p+0) = 3;//printf("*(p+0) = %d\r\n", *p);//*(p+1) = 4;//printf("*(p+1) = %d\r\n", *(p+1));*p = 3;printf("*(p+0) = %d\r\n", *p);*(p+1) = 4;printf("*(p+1) = %d\r\n", *(p+1));}int main(int argc, char *argv[]){//test();//printf("a[0] = %d\r\n", a[0]);//printf("a[1] = %d\r\n", a[1]); int i = 0; for(; i<10; i++) { str.a[i] = i; } str.b = 55; str.c = 66; for(i = 0; i<12; i++) { printf("*p+%d is: %d\r\n", i, *(p+i)); } for(i = 0; i<12; i++) { d[i] = i; } for(i = 0; i<10; i++) { printf("pstr->a[%d] = %d \r\n", i, pstr->a[i]); } printf("pstr->b = %d \r\n", pstr->b); printf("pstr->c = %d \r\n", pstr->c);return 0;}
阅读全文
0 0
- 结构体数组指针的使用
- 数组,指针,结构体的使用
- 从指针的使用角度看数组与结构体
- 实验名称: C++数组、指针、结构体的使用
- 从指针的使用角度看数组与结构体
- 数组,结构体,结构体数组的差别 (使用指针访问元素时)
- 《IOS_C语言》高级指针(结构体指针、结构体数组指针、结构体数组指针,作为函数的参数使用、预编译指令)
- 《IOS_作业C语言》高级指针(结构体指针、结构体数组指针、结构体数组指针,作为函数的参数使用、预编译指令)
- 指针数组 指针的指针 二维数组指针 结构体指针 链表
- 结构体数组指针
- 结构体指针数组
- 结构体指针的使用
- 指向结构体数组的指针
- 指向结构体数组的指针应用
- 指向结构体的指针数组,应用
- 结构体指针与结构体数组的结合。。。
- c++ 结构体练习 结构体重的char数组指针
- 关于结构型数组指针的使用记录
- linux-ls命令参数详解&统计目录下文件和目录的个数实例
- Leetcode#1: twoSum
- spark学习-SparkSQL--12-SparkSession与SparkContext
- App安装过程
- 使用阿里大于发送短信
- 数组,指针,结构体的使用
- C++单例模式
- effective c++ 条款4解读
- 操作前js进行confirm确认
- 纯css hover放大图片
- linux下自创网络编程聊天室(3)
- Filter解决中文乱码问题
- Android:一个妹zhi的学习之路_心得体会
- Android 逆向学习之《Smail语法查询手册》