RTOS第2期C语言问题搜集贴

请大家把自己的疑问在这里贴出来,
2022.08.30晚课集中回答

函数指针在回调函数中的意义和用法?在使用时的注意事项?


#define 是宏定义的意思 为何这里面可以用宏定义的GPIOA用作结构体指针变量并来访问其指向的结构体成员进行赋值

1 个赞

链表的地址或者说是指针也是第一个节点的地址嘛?类似于数组那样吗?

韦老师,请问用source insight查看代码时,怎么快速找到函数指针所指的函数原型,这个比较影响我的阅读效率, :smiling_face_with_tear:如图

它的函数原型是经过各种跳转才找到的

老师,讲解下异步回调函数和同步回调函数的区别,怎么应用?还有程序中回调函数应用多了会使程序看起来更复杂了,逻辑就很难理清楚,我们应该在什么情况下才会去用回调函数?

韦老师能否把您全局、局部变量命名;函数命名等的规范讲下? 比如说变量开头用小写还是大写?函数名开头用小写还是大写?单词之间用大写字母分隔还是用下滑写分隔? 每一次看您的代码有的时候有规范有的时候又没有了规律。

听韦老师的讲课可以学习思考方式,继续上面试题,请韦老师给直播解答下解析过程,可以加深对指针的理解(三种指针知识点:指针±的步长、指针数组、指针字符):
1)

int a[5] = { 1, 2, 3, 4, 5 };
int *ptr = (int *)(&a + 1);//强制类型转换:类型转换,但是值不会改变;
printf( "%d,%d", *(a + 1), *(ptr - 1));
//答案:2 5
	//已知,结构体Test类型的变量大小是20个字节
	p = (struct Test*)0x100000;//p就是结构体类型的指针
	printf("%p\n", p + 0x1);//这里指针类型是20(0x14)字节
	printf("%p\n", (unsigned long)p + 0x1);
    //这里把p转换成整数类型,整型+-1就是直接+-1,所以直接加 0x1
 
	printf("%p\n", (unsigned int*)p + 0x1);//这里指针类型是4(0x4)字节
    //答案:00100014/00100001/00100004

2)

	int a[5][5] = {0};
	int(*p)[4];
	p = (int(*)[4])a;
	printf("%d\n",&p[4][2] - &a[4][2]);//-4  ***重要***:两个地址相减得到是中间元素的个数
	printf("%p\n",&p[4][2] - &a[4][2]);//fffffffc -4以%p的形式打印时,直接将补码以地址的形式读取;
	//以上a[4][2]容易获得
	//p[4][2]的获取,画出a[5][5]在内存中的存储
	//00000 00000 00000 00000 00000
	//p是int(*)[4]型指针,指向a[5][5]第一行第一个元素的地址;
	//p[4]相当于*(p+4),p向后指向了4*4个元素,也就是a[3][1]的位置
	//p[4][2] == (*(p+4))[2] == *((*(p+4))+2)
	//(p+4)是地址,*(p+4)解引用是数组名,且p解引用后*p应该是4个元素的数组,(p+4)也一样
	//所以(*(p+4))[2]向后指2个元素,也就是a[3][3]
	//a[3][3]和a[4][2]中间有4个元素;

3)

	char *c[] = {"ENTER","NEW","POINT","FIRST"};
	char**cp[] = {c+3,c+2,c+1,c};
	char***cpp = cp;
	printf("%s\n", **++cpp);      //point
	printf("%s\n", *--*++cpp+3);  //er
	printf("%s\n", *cpp[-2]+3);   //st
	printf("%s\n", cpp[-1][-1]+1);//ew
	//讲解见笔记:画一下数组的内存分布再做题;
	//注意:++cpp会对cpp重新赋值,相当于cpp = (cpp+1)
	//而cpp[-2]等价于 *(cpp-2),不会cpp重新赋值


为啥最后会自动恢复啊,不太明白

老师,如何理解在形参处的类型转换呢,如:

result = _xxx_acc_set_odr(sensor, (rt_uint32_t)args & 0xffff);

老师,对下面的结构体进行赋值为什么要使用memcpy,直接用“=”不行么。
程序来自于RTT的AP3216C的驱动库(开源),我把关键的部分列出来

int rt_hw_ap3216c_init(const char *name, struct rt_sensor_config *cfg)
{
   rt_sensor_t sensor_als = RT_NULL, sensor_ps = RT_NULL;
   rt_memcpy(&sensor_ps->config, cfg, sizeof(struct rt_sensor_config));
}

相关定义如下:

struct rt_sensor_info
{
    rt_uint8_t     type;                    /* The sensor type */
    rt_uint8_t     vendor;                  /* Vendor of sensors */
    const char    *model;                   /* model name of sensor */
    rt_uint8_t     unit;                    /* unit of measurement */
    rt_uint8_t     intf_type;               /* Communication interface type */
    rt_int32_t     range_max;               /* maximum range of this sensor's value. unit is 'unit' */
    rt_int32_t     range_min;               /* minimum range of this sensor's value. unit is 'unit' */
    rt_uint32_t    period_min;              /* Minimum measurement period,unit:ms. zero = not a constant rate */
    rt_uint8_t     fifo_max;
};

struct rt_sensor_intf
{
    char                       *dev_name;   /* The name of the communication device */
    rt_uint8_t                  type;       /* Communication interface type */
    void                       *user_data;  /* Private data for the sensor. ex. i2c addr,spi cs,control I/O */
};

struct rt_sensor_config
{
    struct rt_sensor_intf        intf;      /* sensor interface config */
    struct rt_device_pin_mode    irq_pin;   /* Interrupt pin, The purpose of this pin is to notification read data */
    rt_uint8_t                   mode;      /* sensor work mode */
    rt_uint8_t                   power;     /* sensor power mode */
    rt_uint16_t                  odr;       /* sensor out data rate */
    rt_int32_t                   range;     /* sensor range of measurement */
};

typedef struct rt_sensor_device *rt_sensor_t;

struct rt_sensor_device
{
    struct rt_device             parent;    /* The standard device */

    struct rt_sensor_info        info;      /* The sensor info data */
    struct rt_sensor_config      config;    /* The sensor config data */

    void                        *data_buf;  /* The buf of the data received */
    rt_size_t                    data_len;  /* The size of the data received */

    const struct rt_sensor_ops  *ops;       /* The sensor ops */

    struct rt_sensor_module     *module;    /* The sensor module */

    rt_err_t (*irq_handle)(rt_sensor_t sensor);             /* Called when an interrupt is generated, registered by the driver */
};

ap3216c-latest.zip (1.7 MB)