Ste*_*e S 2 c arrays pointers function
我只是在学习C,这是我第一次使用stackoverflow,所以我不确定这是否是问这个问题的权利,因为它与周围的其他人相比似乎微不足道,但我在教科书中找到了这段代码,当我尝试在Visual Studio中编译我得到这个:
**error C2040: 'menutext' : 'char *(int)' differs in levels of indirection from 'int ()'**
Run Code Online (Sandbox Code Playgroud)
说实话,我已经查看了代码,但我不知道为什么编译器会抱怨.我真的需要一些帮助.这是代码:
/*********************************************************/
/* */
/* MENU : program which prints out a menu */
/* */
/*********************************************************/
main ()
{
int str_number;
for (str_number = 0; str_number < 13; str_number++)
{
printf ("%s",menutext(str_number));
}
}
/*********************************************************/
char *menutext(int n) /* return n-th string ptr */
{
static char *t[] =
{
" -------------------------------------- \n",
" | ++ MENU ++ |\n",
" | ~~~~~~~~~~~~ |\n",
" | (1) Edit Defaults |\n",
" | (2) Print Charge Sheet |\n",
" | (3) Print Log Sheet |\n",
" | (4) Bill Calculator |\n",
" | (q) Quit |\n",
" | |\n",
" | |\n",
" | Please Enter Choice |\n",
" | |\n",
" -------------------------------------- \n"
};
return (t[n]);
}
Run Code Online (Sandbox Code Playgroud)
您没有对函数进行原型设计menutext(),因此C默认为返回类型int.这将导致printf()抱怨(在你的情况下出错),因为它期望它的第二个arg是类型char *,而不是类型int.
在调用上方添加以下两行main()
#include <stdio.h> /* Needed for the call to printf() */
char *menutext(int); /* Prototype for menutext() */
Run Code Online (Sandbox Code Playgroud)
此外,main()应始终返回类型int,如果您不打算传入任何参数,则应传入void以明确说明该意图.因此,代码的上半部分应如下所示:
#include <stdio.h> /* Needed for the call to printf() */
char *menutext(int); /* Prototype for menutext() */
int main(void)
{
/* main code here */
return 0;
}
Run Code Online (Sandbox Code Playgroud)