0

say i am using a global variable to hold the value of a signal (in a schematics for a circuit board sense) in one function

void randomfunction()
{
    for(t=lnnew,s=node->name;*s;)
    {
    if()
         //some code

    else
      *t=*s;
    t++;
    s++;
    }
printf("%s \n",lnnew);   //so now here lnnew is holding new values of signal and when i print this, every time new value of signal is printed and i have declared it as global
}

now how can i use this global variable inside any other function say writelnnewvalue() so that when ever value of lnnew changes in randomfunction(), it also get changes and printed in writelnnewvalue () function?

I had asked similar type of question in this link , if it seems to be exact copy then mark it as duplicate.

4

2 回答 2

0

您需要在.h包含在所有使用它的编译单元中的头文件(文件)中声明您的变量:

extern volatile int lnnew;

让一个编译单元(.c文件)实际定义它

volatile int lnnew;

然后它将在您包含标题的任何地方都可用。volatile这里向编译器表明该变量会发生不可预知的变化并且不会被缓存。

于 2013-09-04T10:33:05.007 回答
-1

您可以使用两个不同的线程来执行这些任务。使用一个标志,该标志lnnew在一个线程(将运行randomfunction())中更新并使用其他线程(将运行writelnnewvalue())以该标志为条件,以打印所需的任何内容。

您可以在 Internet 上找到有关多线程编程的更多信息。我找到了一些链接:
链接 1
链接 2

链接 1 有带有解释的代码示例。

于 2013-09-04T10:31:43.740 回答