我已经初始化了一个字符串数组。我想通过 while 循环输出各个字符串。我将指向字符串数组的指针传递给函数。 在我当前的版本中,我只定位在单个字符上,我无法得到它..
char * Menuepunkte[] =
{
"Exit",
"Start-Quiz",
"Random-Questions",
"Load-Questions",
"Generator"
};
int MENUE (char * Unterpunkte)
{
while (Unterpunkte != NULL)
{
printf("In: %s len: %i \n", Unterpunkte, strlen(Unterpunkte));
++Unterpunkte;
while(getchar()!='\n');
}
..
最佳答案
您忘记NULL
终止数组,请尝试:
#include <stdio.h>
static void func(char *ptr[])
{
while (*ptr)
{
puts(*ptr);
ptr++;
}
}
int main(void)
{
char *arr[] =
{
"Exit",
"Start-Quiz",
"Random-Questions",
"Load-Questions",
"Generator",
NULL
};
func(arr);
return 0;
}
输出:
Exit
Start-Quiz
Random-Questions
Load-Questions
Generator
所以你的函数应该是这样的:
int MENUE (char *Unterpunkte[]) // or `char **Unterpunkte` (a pointer to pointer)
{
while (*Unterpunkte != NULL)
{
printf("In: %s len: %i \n", *Unterpunkte, strlen(*Unterpunkte));
++Unterpunkte;
...
}
关于c - 如何将 ptr 增加到字符串数组的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66611213/