-3

void functions複数の変数(コード全体で同じ)があるため、関数の個々の出力に依存する複数の関数があり、各関数の出力はそれらに「保存」され、別の関数に渡されます。

そこで、必要なすべてのコードstatic ....の直後にこれらの変数をグローバル変数にすることにしました。#include...

すべての関数 (合計 14 個の関数、 all void) を 4 つ呼び出すだけで利用できました (各関数は、独自の関数を処理した後、結果を別の関数に渡し、一連の受け渡しの後、それらのうちの 4 つだけが必要です。呼ばれるint main()

ここで、他のすべての関数が以前に宣言されたグローバル変数に「コピーして入れた」データに依存してvoid functionいるため、グローバル変数をパラメーターとして必要とする別のものを作成しました。void function(データをグローバル変数に格納することはできないと聞いたので、これは機能していません。)

個々の関数の出力を必要とする一連の関数を作成する他の方法があれば、誰か教えてもらえますか?

変数がきちんと格納されているか確認したのでprintf、#3 の直後のメソッドを使ってみました。からの値が出力されることを期待していたときに、何も出力されないことがわかりました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
}
4

1 に答える 1

1

この関数を見てください:

void askinput(data user)

ここではuser、関数に値を渡します。値渡しの場合、関数は変数のコピーを受け取ります。その関数の本体内で行った変更は、コピーにのみ影響します。それらは呼び出し元の変数には見えません。

代わりに、参照を渡す必要があります。C では、変数へのポインターを渡すことを意味します。

void askinput(data *user)

関数の本体内では、ポインターを逆参照してメンバーにアクセスする必要があります。したがって、メンバーを参照するの->ではなく使用します。.

関数を呼び出すときは、変数へのポインターを渡す必要があります。したがって、呼び出しは次のようになります。

askinput(&user1);

率直に言って、ここでグローバル変数を使用している理由がまったくわかりません。通常、パラメータを渡すことが望ましいです。そうしないと、作業対象の変数のバージョンを追跡するのに苦労することになります。

最後に、プログラム全体を作成しましたが、プログラム全体のコンテキストでこの特定の問題をデバッグしようとすると、混乱します。これを 10 行または 20 行の単純な複製に削減する必要がありました。将来それができるようになると、あなたの人生はずっと楽になります。

于 2013-10-06T15:10:25.787 に答える