新手入门
- 积分
- 7
- 金钱
- 7
- 注册时间
- 2020-8-7
- 在线时间
- 2 小时
|
1金钱
#include "LED.h"
#include "stm32f10x.h"
void LED_init(void)
{
GPIO_InitTypeDef *GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE,ENABLE); //使能时钟 enable disable
GPIO_InitStructure->GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStructure->GPIO_Pin= GPIO_Pin_5;
GPIO_InitStructure->GPIO_Speed= GPIO_Speed_50MHz;//GPIOB_5定为50MHz推挽输出
GPIO_Init(GPIOB,GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5); //PB5输出高
GPIO_InitStructure->GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitStructure->GPIO_Pin= GPIO_Pin_5;
GPIO_InitStructure->GPIO_Speed= GPIO_Speed_50MHz;//GPIOB_5定为50MHz推挽输出
GPIO_Init(GPIOE,GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5); //PE5输出高
}
请问 GPIO_InitTypeDef *GPIO_InitStructure(编译正确);结构体指针为什么不行,写成GPIO_InitTypeDef GPIO_InitStructure(后面相应都改了编译都对)反而可以运行
|
最佳答案
查看完整内容[请看2#楼]
这个问题我在野火那边也回答过,好多人都不清楚。。。
局部变量这是个重点,定义时不赋值直接用它很危险,指针就更危险了。
你定义一个指针但没有实际分配给他一个地址,所以人家是个野指针,可能指null,可能乱指,导致程序乱跑。
你应该在定义指针时malloc一个地址给他,或者之前就定义一个GPIO_InitTypeDef变量,把地址给这个指针(这样就没意义了).
|