我目前被一個問題阻塞,在你看之前,試着認爲我是一個只有初學者試圖使用指針帶功能。我需要一個菜單(函數)誰可以午餐任何其他功能,但我的問題是,我從我的「主」調用我的功能,所以我試圖在我的功能中使用指針。 我只是學習如何使用指針,所以這是我現在明瞭的第一件事。調用一個函數 - 獲得「需要作爲一元運算的左值」和「操作數」
也許這是錯誤的方式,我仍然在互聯網上搜索如何做到或做正確的。
現在,我得到這個錯誤信息:需要
左值作爲一元 '&' 操作數
這裏是我糾正代碼:(簡化了最大的可讀性)
#include <stdio.h>
#include <stdlib.h>
void Menu(char *PointeurNameHuman, int *PointeurChoix, int *PointeurChoix1, int *PointeurChoix2, int *PointeurChoix3, void *PointeurWelcome())
{
printf("\n\nEnter your choice: \n ");
scanf("%d", PointeurChoix);
while ((*PointeurChoix != 1) || (*PointeurChoix != 2) || (*PointeurChoix != 3))
{
if (*PointeurChoix == *PointeurChoix1)
{
printf("\n! Play! \n ");
PointeurWelcome(PointeurNameHuman);
return 0;
}
else if (*PointeurChoix == *PointeurChoix2)
{
return 0;
}
else if (*PointeurChoix == *PointeurChoix3)
{
return 0;
}
}
}
void Welcome(char *PointeurNameHuman)
{
printf("\n\nWelcome %s \nPlease enter your name: ", PointeurNameHuman);
}
int main(int argc, char *argv[])
{
char NameHuman[] = "HUMAN";
int Choix, Choix1 = 1, Choix2 = 2, Choix3 = 3;
char *PointeurNameHuman = &NameHuman;
int *PointeurChoix = &Choix;
int *PointeurChoix1 = &Choix1;
int *PointeurChoix2 = &Choix2;
int *PointeurChoix3 = &Choix3;
void Welcome(PointeurNameHuman);
void (*PointeurWelcome)() = &Welcome; // OLD Error
Menu(PointeurNameHuman,PointeurChoix,PointeurChoix1,PointeurChoix2,PointeurChoix3,PointeurWelcome);
}
關於如何簡化我的代碼的所有評論也歡迎! 在此先感謝您的答案,祝您有個美好的日子。
編輯:非常感謝「b4hand」這個快速的回答,您指出我正確的方向來解決我的問題。
您是否有錯誤消息的行號?這將有助於指出這個問題。 – b4hand 2014-10-07 22:50:46
嘗試通過獲取「void」表達式的地址來形成'void *' – Oktalist 2014-10-07 22:52:29
看起來您需要一個函數指針。 'void(* PointeurWelcome)()=&歡迎(PointeurNameHuman);'。 – 2014-10-07 22:53:03