首页 文章

将指针地址传递给全局变量

提问于
浏览
0

我有一个指针传递给一个像这样的函数:

unsigned char globalvar;

int functionexp(unsigned char *buff){
    globalvar = buff;
    //start interrupt
    //wait for end of interrupt
    //pass original pointer back with updated results
}

void __attribute__((interrupt, no_auto_psv)) _DMA2Interrupt(void) {
    globalvar = somedata;
}

我有一个中断,它收集我需要传递到所述指针的数据 . 我想要做的是创建一个全局虚拟变量并将原始指针(bufF)地址复制到这个全局变量中,所以当我将数据写入全局变量时我可以在中断中访问(因为我无法传递原始变量)指针进入中断)它还会更新原始指针中的值 .

我的示例显示了我想要做的事情的基础,但没有指针语法 . 有人可以告诉我该怎么做,拜托!

1 回答

  • 2

    你的问题并不完全清楚 . 我对你认为你要做的事情的第一个解释是,它会是这样的:

    unsigned char **buffptr;
    
    int functionexp(unsigned char *buff){
        buffptr = &buff;
        //start interrupt
        //wait for end of interrupt
        //pass original pointer back with updated results
        // after this point, the value of buffptr is completely useless,
        // since buff doesn't exist any more!
    }
    
    void __attribute__((interrupt, no_auto_psv)) _DMA2Interrupt(void) {
        *buffptr = somedata;  
        // this changes the 'buff' variable in functionexp
    }
    

    另一方面,你可以简单地说这个:

    unsigned char *globalbuff;
    
    int functionexp(unsigned char *buff){
        globalbuff = buff;
        //start interrupt
        //wait for end of interrupt
        //pass original pointer back with updated results
    }
    
    void __attribute__((interrupt, no_auto_psv)) _DMA2Interrupt(void) {
        globalbuff[0] = somedata;
        // this changes data inside the buffer pointed to by the 'buff' 
        // variable in functionexp
    }
    

相关问题