指针地址和结构体中的数组

23 篇文章 1 订阅
21 篇文章 0 订阅

看看指针和结构体中的数组怎么用的,很基础的,搞清楚一点好。

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <dirent.h>
#include <sys/stat.h>


typedef struct tsmRECT_S {
    unsigned int  s32X;
    unsigned int  s32Y;
    unsigned int  u32Width;
    unsigned int  u32Height;
} RECT_S;
// 输出头文件保存数据格式
typedef struct _BMP_FORMAT_T {
	uint32_t offset; // bmp 在bmp.bin的偏移量
	uint32_t width;  // bmp 宽度
	uint32_t height; // bmp 高度
	uint32_t size;   // bmp 数据大小
	RECT_S  rect[5];
}BMP_FORMAT_T;


BMP_FORMAT_T  tt[10] = {0};

main()
{
	BMP_FORMAT_T *pF;
	
	tt[5].offset = 1;
	tt[5].width = 2;
	tt[5].height = 3;
	tt[5].size = 4;
	tt[5].rect[0].s32X = 11;
	tt[5].rect[0].s32Y = 12;
	tt[5].rect[0].u32Width = 13;
	tt[5].rect[0].u32Height = 14;
	tt[5].rect[1].s32X = 15;
	tt[5].rect[1].s32Y = 16;
	tt[5].rect[1].u32Width = 17;
	tt[5].rect[1].u32Height = 18;	
	pF = &tt[5];
	printf("pF 0x%x\n", pF);
	printf("pF->offset %d\n", pF->offset);	
	printf("pF->width %d\n", pF->width);
	printf("pF->rect[0] %d\n", pF->rect[0]);
	printf("pF->rect[1] %d\n", pF->rect[1]);
	printf("**************************************\n");
	printf("&(pF->offset) 0x%x\n", &(pF->offset));	
	printf("&(pF->width) 0x%x\n", &(pF->width));
	printf("&(pF->size) 0x%x\n", &(pF->size));
	printf("&(pF->rect) 0x%x\n", &(pF->rect));
	printf("&(pF->rect[1]) 0x%x\n", &(pF->rect[1]));
	printf("&(pF->rect[2]) 0x%x\n", &(pF->rect[2]));
	printf("&(pF->rect[3]) 0x%x\n", &(pF->rect[3]));
}

运行结果对比一下:

 pf是一个指针,pf箭头指过去就是取到对应内存的内容了,“pf->”这样就是pf指针带箭头了,如果要取对应那个变量的地址怎么办呢,其实可以看到啊!pf的值就是指针的起始值,看它是不是跟所指结构体的第一个变量的地址是一样的?果然是一样的,成员少,小推一下就知道地址是多少,成员多了就用指针先指过去,然后通过取地址符合&来获取它的地址。

很基础的,看不懂的就要加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值