如何在C中操纵全局变量

时间:2013-10-06 13:39:23

标签: c function global-variables

我有多个void functions依赖于函数的每个单独输出,因为有多个变量(在整个代码中都是相同的),其中每个函数的输出将“存储”到它们并且是传递到另一个。

因此,我决定在所有必要的static ....代码之后立即将这些变量设为#include...,从而将这些变量变为全局变量。

我能够通过只调用其中的四个来利用所有函数(总共14个函数,所有void)(每个函数在处理它自己的函数之后,将结果传递给另一个函数并且经过一系列的传递,int main()

中只需要调用其中的四个

现在,我创建了另一个void function,它需要全局变量作为参数,因为void function依赖于所有其他函数“复制并放入”前面声明的全局变量的数据。 (我发现它不起作用,因为我听说不可能将数据存储到全局变量中。)

如果有任何其他方法可以创建需要输出每个单独功能的系列功能,有人可以教我吗?

我检查了变量是否存储正确,所以我尝试在#3过程之后立即使用printf方法。我发现当我预期要打印struct data的值时,没有任何内容被打印出来。


例如:

typedef struct database{
//... variables
}data;

typedef struct itembase{
//... variables
}item;

static data user1;
static data user2;
static data *pointer[10000];
static item *pointer2[10000];
static item current[10000]; //Shares same value of the bracket with *pointer2
static data sectionA[1][10000];
static data sub_section[3][10000];
static int datacounter = 0; //..will be put inside the bracket of *pointer
static int itemcounter = 0; //..will be put inside the bracket of *pointer2 
static int typenum = 0; ..will be put inside the first bracket of all the sections and subsections
static int section_count = 0; //..will be put inside the second bracket of all sections
static int sub_section_count[3] = {0}; //..will be put inside the second bracket of all sub_sections. The [3] will be the value of the typenum.

void load_data() // Accepts User's input and store them into struct data's variable using singly-linked list
{
//.... All data will be stored to *pointer[datacounter]
binarycheck(pointer[datacounter]->encoding,*pointer,datacounter);
//.... The `typedef struct` of data contains 12 variables. After storing 12 variables, datacounter will be ++ and the program will still continue to accept input from the user
}

void load_item()
{
//.... All item will be stored to *pointer2[itemcounter]
memcpy(&current[itemcounter],pointer2[itemcounter],sizeof(item)); 
}

void binarycheck(data encoding,data *pointer,int datacounter)
{
if ((encoding&128)==128){
typenum = 3; 
memcpy(&sectionA[typenum][section_count],pointer,sizeof(data)); 
sub_sectionA[typenum][sub_section_count[typenum]] = sectionA[typenum[section_count]; 
section_count++;
sub_section_count++;
}
}

void askitem(data user)
{
// Tried putting `printf(" %s User1 Data#1",user1.firstdata);` and it works perfectly fine.
// Ask for user's selection on item
// If the item is found, then the content of that item will modify the data of the variable of `user`
}

void askinput(data user) 
{ 
int whattype = 0;
int whatsub = 0;

  printf("What type do you want?: \n);
  scanf("%d",&whattype);
  if (whattype == 1)
  {typenum = 1;}
  printf("What Sub type do you want?: \n);
  scanf("%d",&whatsub);
  if (whatsub == 1)
  { user = sub_sectionA[typenum][sub_section_count[typenum]];}
  askitem(user);
} 

void final_print(data user, data user2)
{
printf("%d\n",user.Adata);
printf("%d\n",user2.Adata);
}

int main()
{
load_data();
load_item();
askinput(user1);
//Tried putting `printf(" %s User1 Data#1",user1.firstdata);` but nothing shows.
askinput(user2);
//Nothing shows
final_print(user1,user2); //Nothing shows
}

1 个答案:

答案 0 :(得分:1)

看看这个功能:

void askinput(data user)

在这里,您将user按值传递给函数。传递值时,函数会收到变量的副本。您在该函数体内进行的更改仅影响副本。它们对调用者的变量不可见。

相反,您需要传递引用。在C中,这意味着将指针传递给变量:

void askinput(data *user)

在函数体内,您需要取消引用指向访问成员的指针。因此,您使用->而不是.来引用成员。

当你调用函数时,你需要传递一个指向变量的指针。所以电话会变成:

askinput(&user1);

坦率地说,我不明白为什么你在这里使用全局变量。通常最好传递参数,否则你会发现自己很难跟踪你想要处理的变量的哪个版本。

最后,您编写了整个程序,并尝试在整个程序的上下文中调试此特定问题让您感到困惑。你真的应该把它减少到10或20行简单再现。能够在将来做到这一点将使您的生活更加轻松。