[英]Assign values to a global struct from a function
我正在從一本書中學習 C,為了更好地理解這門語言,我嘗試了各種方法。 我正在嘗試將值從 function 傳遞到全局結構。 我遇到了麻煩,因為我執行了printf
來查看值是否確實通過了,但結果顯示了不同的值:
#include <stdio.h>
void receive_date(); //prototype of the function
struct date_example {
int day;
int month;
int year;
};
int main()
{
struct date_example d;
receive_date();
//this line below shows different values than those provided by the scanf inside the function
printf("the day is %d and the month is %d and the year is %d",d.day,d.month,d.year);
return 0;
}
void receive_date(void)
{
struct date_example d;
printf("give day: \n");
scanf("%d", &d.day);
printf("give month: \n");
scanf("%d", &d.month);
printf("give year: \n");
scanf("%d", &d.year);
printf("the date is: %.1d %.1d %.1d \n", d.day, d.month, d.year );
}
結果如下:
give day:
2
give month:
3
give year:
2021
the date is: 2 3 2021
the day is 32758 and the month is 0 and the year is 0 // this is the part that gives different values
Process finished with exit code 0
您沒有全局結構,即沒有全局和結構類型的變量。 只有類型聲明是“全局的”。
更明確地說,你有這行兩次,每個 function 一次。
struct date_example d;
main()
中的那個是一個局部變量,只能從main()
訪問。
receive_date
里面的一個在那里是一個本地的,這是從 function 中提到它的所有行訪問的那個。
因此,無論您在receive_date
中做什么都會影響本地,但不會影響main()
本地的另一個。
你 printf 在 function 里面從d
local 到 function 是不相關的,在 main() 中/從main()
中是不可見的。
如果您確實希望在第二個 function 中對d
所做的任何操作都具有從main()
可見的效果,那么您需要刪除這兩行相同的行並在兩個函數之外創建一個,即在main()
和在另一個 function 之外。它將定義一個全局d
,可以從兩個函數訪問它。
但是,“您需要”僅指您的學習實驗。
實際上不推薦使用全局變量的設計。
您最好使用返回值,或者(可能比您現在可能稍微高級一點)指向變量的指針參數,以便從 function 中進行操作。
這只會創建結構,但不會創建結構變量:
struct date_example {
int day;
int month;
int year;
};
然后,您的代碼在自動空間(本地范圍)中定義結構變量:
int main()
{
struct date_example d;
...
}
要使其成為全球性的,只需將此語句放在您的結構定義之后:
struct date_example {
int day;
int month;
int year;
};
struct date_example d;
然后修改以下內容以使用新的全局......
int main()
{
//struct date_example d;//already defined in global space
receive_date();
...
}
void receive_date(void)
{
//struct date_example 'd', already defined in global space.
printf("give day: \n");
scanf("%d", &d.day);
printf("give month: \n");
scanf("%d", &d.month);
printf("give year: \n");
scanf("%d", &d.year);
printf("the date is: %.1d %.1d %.1d \n", d.day, d.month, d.year );
}
警告
綜上所述,使用全局變量並不總是最好的方法。 盡管全局變量有其用武之地,但應謹慎使用。 考慮將指針傳遞給您的結構實例作為替代方案。 這是如何...
//assuming same struct definition as before...
//change prototype to accept pointer instance of struct date_example
void receive_date(struct date_example *d);
int main(void)
{
struct date_example date = {0};//define instance of struct date_example here
receive_date(&date);//pass address of 'date'
printf("the date is: %.1d %.1d %.1d \n", date.day, date.month, date.year );
return 0;//use a return statement to exit program
}
void receive_date(struct date_example *d)
{
//using pointer argument d
printf("give day: \n");
scanf("%d", &d->day);//note change from . to -> for pointer notation
printf("give month: \n");
scanf("%d", &d->month);
printf("give year: \n");
scanf("%d", &d->year);
//to illustrate, call printf in main()
//printf("the date is: %.1d %.1d %.1d \n", d.day, d.month, d.year );
}
從 function 為全局結構賦值
對於初學者,您既沒有結構類型的全局 object。
在您的程序中,您有兩個具有自動存儲持續時間的結構類型的本地對象。 這意味着它們在定義它們的范圍之外不存在。
在 function receive_date
void receive_date(void)
{
struct date_example d;
printf("give day: \n");
scanf("%d", &d.day);
printf("give month: \n");
scanf("%d", &d.month);
printf("give year: \n");
scanf("%d", &d.year);
printf("the date is: %.1d %.1d %.1d \n", d.day, d.month, d.year );
}
您初始化了它的局部變量d
,該變量在 function 之外不可見且處於活動狀態。
在 main 中,您正在嘗試 output 未初始化的 object d 的數據成員
int main()
{
struct date_example d;
receive_date();
//this line below shows different values than those provided by the scanf inside the function
printf("the day is %d and the month is %d and the year is %d",d.day,d.month,d.year);
return 0;
}
所以代碼調用了未定義的行為。
您可以通過以下方式更新程序
void receive_date( struct date_example *d );
注意,在這種情況下,您需要將結構體定義移動到 function 聲明之前。
function main 看起來像
int main()
{
struct date_example d;
receive_date( &d );
//this line below shows different values than those provided by the scanf inside the function
printf("the day is %d and the month is %d and the year is %d",d.day,d.month,d.year);
return 0;
}
function receive_date 的定義如下
void receive_date( struct date_example *d )
{
printf("give day: \n");
scanf("%d", &d->day);
printf("give month: \n");
scanf("%d", &d->month);
printf("give year: \n");
scanf("%d", &d->year);
printf("the date is: %.1d %.1d %.1d \n", d->day, d->month, d->year );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.