Microchip
直播中

陈伟

7年用户 1521经验值
私信 关注
[问答]

指向Struct问题的指针抛出异常

大家好,我想在我的代码中使用指针来构造,因为我有使用这个表单的库。我遇到的问题是,当我试图访问结构中的一个成员时,就会抛出异常。我用XC32编译器使用PIC32 MM0256GPM028。这里是我的代码(注意,这是生成异常的最简单的代码形式),在这里,当我尝试将Tun1成员设置为0时,出现异常。有趣的是,我尝试了同样的代码在PICT32 MX795F512L中,没有例外发生并且工作良好。有什么评论吗?提前感谢。

以上来自于百度翻译


      以下为原文

    Hi all,

I am trying to use Pointer to Struct in my code since I have library which uses that form. The problem I encounter is when ever I try to access a member in the structure a exception is thrown. I am using PIC32MM0256GPM028 with XC32 compiler. Here is my code (note this is the simplest form of code which generate the exception)

#include "mcc_generated_files/mcc.h"
#include "common.h"
#include "mcc_generated_files/spi2.h"
#include

unsigned char rx_buffer;


typedef struct{
   
    unsigned int temp1;
    unsigned char temp2;
   
   
}FIELD_UNIT;

FIELD_UNIT *Field_Unit;



/*
                         Main application
*/
int main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalDisable();

   
    while(1)
    {
   
    //IO_RB9_Toggle();
        Field_Unit->temp1=0; //<---At this point exception occurs.
   
    }

   
}
/**
End of File


Here, I when I try to set the temp1 member to zero an exception occur. Interestingly, I tried the same code in PIC32MX795F512L and no exceptions occur and works fine.
Any comments?

Thanks in advance.

回帖(6)

吴键洪

2018-11-13 15:48:05
TyEndof实际上不创建变量,它只是生成一个可以用来创建变量的名称。该代码创建了一个指向FieldIyType单元的变量的指针,但是在该代码中的任何地方都没有字段FieldUngy变量的定义,因此指针仍然是空的。

以上来自于百度翻译


      以下为原文

    A typedef doesn't actually create a variable, it just makes a name you can then use to create variables.
That code creates a pointer to a variable of type FIELD_UNIT, but there is no definition of a FIELD_UNIT variable anywhere in that code, so the pointer will still be NULL.
 
 
举报

杨玲

2018-11-13 15:56:47
您的问题与结构无关,错误是声明一个指针,而不是设置指针指向任何东西。你需要一些类似的东西:FieldIn单位S;FieldIn单位=&;

以上来自于百度翻译


      以下为原文

    Your problem isn't really related to structs; the error is that you're declaring a pointer and you don't set it to point to anything.  You need something like:
 
FIELD_UNIT s;
Field_Unit = &s;
举报

云解相

2018-11-13 16:02:07
谢谢你的回复。然而,我并不清楚。你能用小代码片段给我看看吗?

以上来自于百度翻译


      以下为原文

    Thanks for the reply. However it is not clear to me. Can you just show it to me in small code snippet.?
举报

吴键洪

2018-11-13 16:10:02
只需添加两行诗句给你,直接在源代码中的这行之后。

以上来自于百度翻译


      以下为原文

    Just add the two lines jtemples gave you, straight after this line in your source
 
FIELD_UNIT *Field_Unit;
 
举报

更多回帖

发帖
×
20
完善资料,
赚取积分