C语言 在 c 中定义和迭代字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6812242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:13:36  来源:igfitidea点击:

defining and iterating through array of strings in c

c

提问by user391986

How can I define an array of string in c then iterate with a loop through the items in the array?

如何在 c 中定义一个字符串数组,然后循环遍历数组中的项目?

So far I have

到目前为止我有

char myStrings[][10] = { "one", "two", "three", "four", "five" };
// do I need to specify the "10" maximum length?
// also does it automatically create a null ending character after the string?

int i = 0;
for( i = 0; i < ; i++)
{
// I need to pass each string to  a function which accepts
// const char *
}

回答by MByD

When you declare a char sequence with "", null terminator is added.

当您使用 声明字符序列时"",将添加空终止符。

char myStrings[][10] = { "one", "two", "three", "four", "five" };

size_t i = 0;
for( i = 0; i < sizeof(myStrings) / sizeof(myStrings[0]); i++)
{
    fooThatReceivesOneString(myStrings[i]);
}


Edit - sizeof()

编辑 - sizeof()

sizeof() returns the size of a variable. It doesn't matter if the variable is an int, array or 2d array.

sizeof() 返回变量的大小。变量是整数、数组还是二维数组都没有关系。

For example, see the following program

例如,看下面的程序

#include <stdio.h>
int main() {
    char myStrings[][10] = { "one", "two", "three", "four", "five" };
    printf("sizeof(myStrings):    %zu\n", sizeof(myStrings));
    printf("sizeof(myStrings[0]): %zu\n", sizeof(myStrings[0]));
    return 0;
}

Which outputs (on my machine):

哪些输出(在我的机器上):

sizeof(myStrings):    50
sizeof(myStrings[0]): 10

Since each element in the array has the same size, we can divide the sizeof an array with the sizeof the first element to get the amount of elements in the array.

由于数组中每个元素的大小都相同,因此我们可以将数组的大小除以第一个元素的大小,以得到数组中元素的数量。

回答by Engineer

void loopftn (void)
{
  char *numbers[] = {"One", "Two", "Three", ""}, **n;

  n = numbers;
  while (*n != "") {
    printf ("%s\n",  *n++);
  }
  return;
}
  1. You don't need to have a maximum length.
  2. All strings in C are null-terminated.
  1. 您不需要有最大长度。
  2. C 中的所有字符串都以空字符结尾。

回答by iammilind

do I need to specify the "10" maximum length?

我需要指定“10”最大长度吗?

Yes, apart from the 1st dimension of the array you need to mention all the subsequent dimensions

是的,除了数组的第一个维度外,您还需要提及所有后续维度

lso does it automatically create a null ending character after the string?

它是否会在字符串后自动创建一个空结尾字符?

Yes

是的

I need to pass each string to a function which accepts const char *

我需要将每个字符串传递给一个接受 const char * 的函数

You can pass each string like this:

您可以像这样传递每个字符串:

for( i = 0; i < ; i++)
{
  foo(myStrings[i]);
}

Also, You can choose between const char*and char*; since you have declare this as an array it's modifyable; had it been something like

此外,您可以在const char*和之间进行选择char*;由于您已将其声明为数组,因此它是可修改的;如果它是这样的

const char *mystrings[] = { " ", " "}; // string literal

then you must have to pass it as const char*because string literals should always be const char*

那么你必须传递它,const char*因为字符串文字应该总是const char*

回答by Hyperboreus

ad 1) You need to specify a length

广告 1) 您需要指定长度

ad 2) Yes, string literals are null-ended.

广告 2) 是的,字符串文字是以空结尾的。

Inside your for, just call the function with parameter myStrings[i].

在您的 for 中,只需调用带有参数 myStrings[i] 的函数。

回答by user2195463

In C the statement as shown above **n != "" , is illegal at first sight. It compares a pointer with a string. Even *n != "", would compare the pointer of the string with the "" stack string pointer, not the strings. Should use strcmp or compare first character **n=='\0' or **n==0. Also +*n increments the character int the pointed string, not the pointer to string...

在 C 中,如上所示的 **n != "" 语句乍一看是非法的。它将指针与字符串进行比较。即使 *n != "",也会将字符串的指针与 "" 堆栈字符串指针进行比较,而不是字符串。应该使用 strcmp 或比较第一个字符 **n=='\0' 或 **n==0。另外 +*n 会增加指向字符串的字符 int ,而不是指向字符串的指针......

Here is a good implementation:

这是一个很好的实现:

Code:

代码:

static const char* strings[]={"asdf","asdfasdf",0};
const char** ptr = strings;
while(*ptr != 0)
{
   printf("%s \n", *ptr);
   ++ptr;
}

回答by Hossein Momen

This might help

这可能有帮助

int main()
{
  int upp;

  int num;
  char password[20];
  int i;

  printf("Enter a Password (Must include a special char, a number, and an uppercase letter): \n");
  scanf(" %s", password);

  for (i=0;i<strlen(password);i++){
    if (isupper(password[i])){
      upp=1;

    }else if (isdigit(password[i])){
      num=1;
    }
  }if (num==1 && upp==1){
    printf("u may enter");
  }else
  printf("try again");

  return 0;
}

回答by Dov Grobgeld

Yes, you need to specify the length, or add a NULL entry as the last entry in your string array. C does not do that automatically for you.

是的,您需要指定长度,或者添加一个 NULL 条目作为字符串数组中的最后一个条目。C 不会自动为您执行此操作。

回答by John

I always do it that way, seems elegant and easy to me.
You just define an array as usual, without fixed index.
All you have to make sure is to add a single 0 at the end.
Then you can make the shortest TEST case possible and iterate through it with for or while.

我总是这样做,对我来说似乎优雅而轻松。
您只需像往常一样定义一个数组,没有固定索引。
您所要做的就是在末尾添加一个 0。
然后,您可以制作最短的 TEST 案例,并使用 for 或 while 对其进行迭代。

Here is an example, it displays a list of options and the fitting help element (in that case the help array needs to be at least as long as the options one.

这是一个示例,它显示一个选项列表和合适的帮助元素(在这种情况下,帮助数组需要至少与选项一样长。

const char *options[]={
    "CAL_MAG_MIN",
    "CAL_MAG_MAX",
    0
    };
const char *help[]={
    "<X,Y,Z>",
    "<X,Y,Z>",
    0
    };

int pos;
for (pos=0;options[pos];pos++)
{
    printf("\t %s %s\n",options[pos],help[pos]);
}

回答by Mehmet Fatih Tabak

Try this..

尝试这个..

int j =0;
while (char myStrings[][j] !='##代码##'){
  i++;
}
for (i = 0; i < j; i++)    {
  somemethod(myStrings[][i]);
}