搜索
bottom↓
回复: 7

结构体参数传入的问题,感谢大神看看问题在哪里?

[复制链接]

出0入0汤圆

发表于 2019-2-26 15:37:11 | 显示全部楼层 |阅读模式
完整代码如下:

#include "stdafx.h"

typedef unsigned short     int uint16_t;
uint16_t GPIO_Pin_1=1;
uint16_t GPIO_Pin_2=2;

typedef struct GPIO_TypeDef
{
  uint16_t a;      
};

struct GPIO_CHECKSELF
{
        GPIO_TypeDef GPIOx;
        uint16_t GPIO_Pin_x;
};
struct GPIO_CONTROL
{
        GPIO_TypeDef GPIOx;
        uint16_t GPIO_Pin_x;
};

struct DEVICE
{
        GPIO_CHECKSELF GPIO_Checkself;
        GPIO_CONTROL GPIO_Control;
};

void ConfigDevice(DEVICE device,GPIO_TypeDef GPIOx,uint16_t GPIO_Pin_x,GPIO_TypeDef GPIOy,uint16_t GPIO_Pin_y)
{
        device.GPIO_Checkself.GPIOx = GPIOx;
        device.GPIO_Checkself.GPIO_Pin_x = GPIO_Pin_x;

        device.GPIO_Control.GPIOx = GPIOy;
        device.GPIO_Control.GPIO_Pin_x = GPIO_Pin_y;
}


int _tmain(int argc, _TCHAR* argv[])
{
        DEVICE KeTingLight;
        GPIO_TypeDef GPIOC;
        GPIOC.a =1;
        ConfigDevice(KeTingLight,GPIOC,GPIO_Pin_2,GPIOC,GPIO_Pin_1);
        printf("%d,%d,%d,%d:\r\n",KeTingLight.GPIO_Checkself.GPIOx,KeTingLight.GPIO_Checkself.GPIO_Pin_x,KeTingLight.GPIO_Control.GPIOx,KeTingLight.GPIO_Control.GPIO_Pin_x);
        printf("完成:\r\n");
        return 0;
}

输出结果应该是 1 2 1 1
结果输出的是 52428 52428 52428 52428


出0入25汤圆

发表于 2019-2-26 17:58:49 | 显示全部楼层
函数参数更改要带出来,该用指针啊。。。

出870入263汤圆

发表于 2019-2-26 19:01:01 | 显示全部楼层
C语言没学过吗?传值跟传址都没搞懂!

出0入0汤圆

发表于 2019-2-26 19:08:01 | 显示全部楼层
本帖最后由 g921002 于 2019-2-26 19:09 编辑

錯誤不少,下面改一下。另外在printf看GPIO_TypeDef沒有意義。
這個C語言有待加強啊!!

真的跑在STM32,怎麼跑飛的或死機都不知道。XD


  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef unsigned short int uint16_t;
  4. uint16_t GPIO_Pin_1=1;
  5. uint16_t GPIO_Pin_2=2;

  6. typedef struct
  7. {
  8.   uint16_t a;
  9. }GPIO_TypeDef;

  10. typedef struct
  11. {
  12.         GPIO_TypeDef GPIOx;
  13.         uint16_t GPIO_Pin_x;
  14. }GPIO_CHECKSELF;

  15. typedef struct
  16. {
  17.         GPIO_TypeDef GPIOx;
  18.         uint16_t GPIO_Pin_x;
  19. }GPIO_CONTROL;

  20. typedef struct
  21. {
  22.         GPIO_CHECKSELF GPIO_Checkself;
  23.         GPIO_CONTROL GPIO_Control;
  24. }DEVICE;


  25. void ConfigDevice(DEVICE *device,GPIO_TypeDef GPIOx,uint16_t GPIO_Pin_x,GPIO_TypeDef GPIOy,uint16_t GPIO_Pin_y)
  26. {
  27.         device->GPIO_Checkself.GPIOx = GPIOx;
  28.         device->GPIO_Checkself.GPIO_Pin_x = GPIO_Pin_x;

  29.         device->GPIO_Control.GPIOx = GPIOy;
  30.         device->GPIO_Control.GPIO_Pin_x = GPIO_Pin_y;
  31. }
  32. int main()
  33. {
  34.         DEVICE KeTingLight;
  35.         GPIO_TypeDef GPIOC;
  36.         GPIOC.a =1;
  37.         ConfigDevice(&KeTingLight,GPIOC,GPIO_Pin_2,GPIOC,GPIO_Pin_1);
  38.         printf("%d,%d:\r\n",
  39.                KeTingLight.GPIO_Checkself.GPIO_Pin_x,
  40.                KeTingLight.GPIO_Control.GPIO_Pin_x);
  41.         printf("完成:\r\n");
  42.         return 0;
  43. }
复制代码

出0入93汤圆

发表于 2019-2-26 20:09:50 | 显示全部楼层
void ConfigDevice(DEVICE device,GPIO_TypeDef GPIOx,uint16_t GPIO_Pin_x,GPIO_TypeDef GPIOy,uint16_t GPIO_Pin_y)
{
改为
void ConfigDevice(DEVICE &device,GPIO_TypeDef GPIOx,uint16_t GPIO_Pin_x,GPIO_TypeDef GPIOy,uint16_t GPIO_Pin_y)
{

如果您真的不懂传值、传址和传引用的区别的话,建议不要轻易用C++,比C的坑更多。

出0入0汤圆

 楼主| 发表于 2019-2-26 22:03:29 | 显示全部楼层
armstrong 发表于 2019-2-26 19:01
C语言没学过吗?传值跟传址都没搞懂!

几年没碰了,最近家里要装修,所以自己搞点东西的,尴尬

出0入0汤圆

 楼主| 发表于 2019-2-26 22:04:07 | 显示全部楼层
takashiki 发表于 2019-2-26 20:09
void ConfigDevice(DEVICE device,GPIO_TypeDef GPIOx,uint16_t GPIO_Pin_x,GPIO_TypeDef GPIOy,uint16_t G ...

感谢,感谢,很多年没用了

出0入0汤圆

 楼主| 发表于 2019-2-26 22:04:36 | 显示全部楼层
g921002 发表于 2019-2-26 19:08
錯誤不少,下面改一下。另外在printf看GPIO_TypeDef沒有意義。
這個C語言有待加強啊!!

感谢感谢,几年没碰了,最近家里要装修,所以自己搞点东西的,尴尬
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-5 16:03

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表