c - 如何将 ptr 增加到字符串数组的第一个元素

标签 c

我已经初始化了一个字符串数组。我想通过 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/

相关文章:

c++ - I2C-dev : Change write and read parameters

c - 十六进制字符串格式

在 C 中创建列表的整数列表

c++ - 我可以将哪些同步原语用于 clone(2) (C/C++)?

c - execvpe argv 需要参数匹配语法帮助

c++ - 如何在一个指针数组中存储可变大小的空指针?

c - 如何定义编译时 -D 宏 (Apache 2.2)?

c - 基于这个严格的例子,如何在 C 编程语言中将字符串转换为整数?

c - 数独 9 个盒子 ( 3x3 ) C 中的递归回溯所有组合

c++ - 使用 fork() 的子和父 pid;