C语言编程,在声明函数时,我们可以用char、int、float等类型变量作为变量,也可用其指针类型做参变量。如果用指针变量做函数参数,在调用函数时正确给参变量赋值,不会出现问题,如果在给参变量赋值类型不匹配,可能不会报错,但结会不出错。先看下面例程:
头文件:
/*main.h
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu ,01/15/2023
*/
#ifndef __MAIN_H__
#define __MAIN_H__
#include "myport.h"
#include "mtype.h"
#include "config.h"
#include "STC32G_GPIO.h"
#include "STC32G_Delay.h"
#include "STC32G_UART.h"
#include "STC32G_EEPROM.h"
//#include "STC32G_PWM.h"
//#include "STC32G_ADC.h"
//#include "STC32G_EEPROM.H"
//#include "STC32G_SPI.h"
#include "STC32G_PWM.h"
//#include "STC32G_Timer.h"
//#include "STC32G_comparator.h"
void swap(i8* x, i8* y);
源文件:
/*main.c
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu, 01/15/2023
*/
#include "main.h"
ui8 str[30] = {0};
i8 t1 = 105;
i8 t2 = 115;
void main()
{
SysInit();
Uart1_Init(VBAUD_8BITS,G1, 0, 9600);
swap(&t1, &t2);
while(1)
{
LongToString(t1,str);
Uart1_SendString(str);
Uart1_SendString("\r\n");
LongToString(t2,str);
Uart1_SendString(str);
Uart1_SendString("\r\n");
Uart1_SendString(" ");
Uart1_SendString("\r\n");
Delayxms(1000);
}
}
//End of main()
void swap(i8* x, i8* y)
{
i8 tem = *x;
*x = *y;
*y = tem;
}
编译该程序,将其下载到STC32G单片机,打开串口助手,可看到如下结果:
![](https://img-blog.csdnimg.cn/img_convert/3e5b9f505fe0a415f7385495230f5603.png)
从串口助手收到的结果可以看出,实现了t1、t2值的交换,程序运行正常。下面修改变量t1、t2的声明,修改如下:
![](https://img-blog.csdnimg.cn/img_convert/d27ecb0644d90a2e6d95a2984fa8a94f.png)
编译该程序,结果如下:
![](https://img-blog.csdnimg.cn/img_convert/3812712afb55a537e24a08ea5d6618a9.png)
可以看出编译没有报错。现在将其下载到STC32G单片机,打开串口助手,看到的结果如下:
![](https://img-blog.csdnimg.cn/img_convert/d96cca57788e1599f0ffd992469686e1.png)
从串口助手可以看出,并未正确实现t1与t2的值的交换。编译时不报错,运行结果不正确,这就比较麻烦。下面修改一下函数声明及函数源码,看下修改后结果又如何。修改后的代码如下:
头文件:
/*main.h
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu ,01/15/2023
*/
#ifndef __MAIN_H__
#define __MAIN_H__
#include "myport.h"
#include "mtype.h"
#include "config.h"
#include "STC32G_GPIO.h"
#include "STC32G_Delay.h"
#include "STC32G_UART.h"
#include "STC32G_EEPROM.h"
//#include "STC32G_PWM.h"
//#include "STC32G_ADC.h"
//#include "STC32G_EEPROM.H"
//#include "STC32G_SPI.h"
#include "STC32G_PWM.h"
//#include "STC32G_Timer.h"
//#include "STC32G_comparator.h"
void swap(i32* x, i32* y);
#endif
源文件:
/*main.c
Designed by Bill Liu
Version 0.0
Modified last by Bill Liu, 01/15/2023
*/
#include "main.h"
ui8 str[30] = {0};
i16 t1 = 105;
i16 t2 = 115;
void main()
{
SysInit();
Uart1_Init(VBAUD_8BITS,G1, 0, 9600);
swap(&t1, &t2);
while(1)
{
LongToString(t1,str);
Uart1_SendString(str);
Uart1_SendString("\r\n");
LongToString(t2,str);
Uart1_SendString(str);
Uart1_SendString("\r\n");
Uart1_SendString(" ");
Uart1_SendString("\r\n");
Delayxms(1000);
}
}
//End of main()
void swap(i32* x, i32* y)
{
i32 tem = *x;
*x = *y;
*y = tem;
}
编译该程序,将其下载到STC32G单片机,打开串口助手,可看到如下结果:
![](https://img-blog.csdnimg.cn/img_convert/76c806b6a86df4c0890a9316b767cfb6.png)
从串口助手收到的结果可以看出,实现了t1、t2值的交换,程序运行正常,由于Swap函数是以指针变量作为参数,如果将变量取址赋值与函数的指针变量,必须考虑到地址完整性问题与大小端对齐,否则结果会出错,且编译不会报错。我们在声明函数时就要尽量避开指针陷阱。如我们声明获取STC32G ADC结果的函数,可以采用如下声明:u16 STC32G_AdcGetRes(); 而尽量避免使用
void STC32G_AdcGetRes(u16 *pResult);