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

时间:2013-07-11 22:00:09

标签: c variables pointers global

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

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 个答案:

答案 0 :(得分: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
}