這個問題在這里已經有了答案: 我什么時候應該在 scanf() 3 答案 中使用 & 符號 昨天關門。
我試圖理解為什么 scanf() 函式中的第三個引數需要一個 & 號而不是第二個,如果沒有 & 號,我會收到一條錯誤訊息,說“scanf() 的引數 3 不是指標”。
struct person {
int age;
float weight;
char name[30];
};
for(i = 0; i < n; i)
{
printf("Enter name of person %d and age respectively: ", i);
// To access members of 1st struct person,
// ptr->name and ptr->age is used
// To access members of 2nd struct person,
// (ptr 1)->name and (ptr 1)->age is used
scanf("%s %d", (ptr i)->name, &(ptr i)->age);
}
我只是想了解為什么會發生這種情況,而我也無法真正掌握箭頭指標運算子。
uj5u.com熱心網友回復:
當您洗掉指標取消參考時,您會得到類似于此的內容:
int age;
char name[30];
scanf("%s %d", name, age);
顯然,age論點只是一個int,并且不能被改變。1 scanf需要能夠改變它,因此可變引數的 C 約定是將其作為指標傳遞,或者&age在這種情況下。
當擴展到您的原始示例時&(ptr[i]->age),您會得到一些可以表示為更輕松的內容&ptr[i]->age,因為兩者都[]具有->更高的優先級&。
--
1對引數值的修改是本地的。通過指標進行的修改會影響原始值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/520281.html
標籤:C运营商符号
