Keil C 编程时函数声明时慎用指针变量做参变量

48 篇文章 19 订阅

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单片机,打开串口助手,可看到如下结果:

从串口助手收到的结果可以看出,实现了t1、t2值的交换,程序运行正常。下面修改变量t1、t2的声明,修改如下:

编译该程序,结果如下:

可以看出编译没有报错。现在将其下载到STC32G单片机,打开串口助手,看到的结果如下:

从串口助手可以看出,并未正确实现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单片机,打开串口助手,可看到如下结果:

从串口助手收到的结果可以看出,实现了t1、t2值的交换,程序运行正常,由于Swap函数是以指针变量作为参数,如果将变量取址赋值与函数的指针变量,必须考虑到地址完整性问题与大小端对齐,否则结果会出错,且编译不会报错。我们在声明函数时就要尽量避开指针陷阱。如我们声明获取STC32G ADC结果的函数,可以采用如下声明:u16 STC32G_AdcGetRes(); 而尽量避免使用

void STC32G_AdcGetRes(u16 *pResult);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bill66

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值