简体   繁体   English

如何修复此代码以创建字符串数组?

[英]How do I fix this code to create an array of strings?

I want to create an array of strings. 我想创建一个字符串数组。 Here is the code: 这是代码:

#include <stdio.h>
int main(void){
  char str1[] = {'f','i'};
  char str2[] = {'s','e'};
  char str3[] = {'t','h'};
  char arry_of_string[] = {str1,str2,str3};
  printf("%s\n",arry_of_string[1]);
  return 0;
}

This is the line that doesn't work: 这是不起作用的行:

char arry_of_string[] = {str1,str2,str3}; 

How do I correct it? 我该如何纠正?

If you would like to create an array of strings , you are missing an asterisk, and terminating zeros: 如果要创建字符串数组,则缺少星号,并终止零:

char str1[] = {'f','i','\0'};
char str2[] = {'s','e','\0'};
char str3[] = {'t','h','\0'};
char *arry_of_string[] = {str1,str2,str3};

There is an easier way of doing the individual strings, too: 还有一种更简单的方法来处理单个字符串:

char str1[] = "fi";
char str2[] = "se";
char str3[] = "th";
char *arry_of_string[] = {str1,str2,str3};

When you use the char x[] = "..." construct, the content of your string literal (which includes a terminating zero) is copied into memory that you are allowed to write, producing the same effect as char x[] = {'.', '.', ... '\\0'} construct. 当您使用char x[] = "..."构造时,字符串文字的内容(包括终止零)将被复制到您允许写入的内存中,产生与char x[] = {'.', '.', ... '\\0'}相同的效果char x[] = {'.', '.', ... '\\0'}构造。

you can use it like this:(notice that if you want to print an array of char, u must have it termitated by '\\0') 你可以像这样使用它:(请注意,如果你想打印一个char数组,你必须用'\\ 0'来表示它)

int main()
{
    char str1[] = {'f','i','\0'};
    char str2[] = {'s','e','\0'};
    char str3[] = {'t','h','\0'};
    char* arry_of_string[] = {str1,str2,str3};

    for (int i=0;i<3;i++)
    {
        printf("%s\n",arry_of_string[i]);

    }
    return 0;

}

You are probably looking for a pointer here rather than a direct array. 您可能在这里寻找指针而不是直接数组。

char *arry_of_string[] = {str1,str2,str3};

An array is a collection of values, a pointer is a list of addresses containing values, so a char pointer is a pointer to the address of the arrays containing your strings (character arrays). 数组是值的集合,指针是包含值的地址列表,因此char指针是指向包含字符串(字符数组)的数组地址的指针。 and breathe 呼吸

you could just use: 你可以使用:

const char *array_of_string[] = {"fi", "se", "th"};

int i;
for (i=0;i<3;i++) {
    printf("%s\n", array_of_string[i]);
}

if you want to be concise... 如果你想简洁......

When you specify the string as 将字符串指定为时

char* myString = "abcd";

It creates a pointer of type array and points to the base address or the 0th element of the character array. 它创建一个类型为array的指针,并指向基本地址或字符数组的第0个元素。 So myString points to a. 所以myString指向一个。 Using a char* is useful because c provides a good way of printing out the entire array using 使用char *非常有用,因为c提供了一种使用打印整个数组的好方法

printf("%s",myString);

Also pointer is useful to use when you dont know or dont want to specify the length of the char array. 当您不知道或不想指定char数组的长度时,指针也很有用。 Your question should be solved if you do this 如果你这样做,你的问题应该解决

  char *str1 = "fi";
  char *str2 = "se";
  char *str3 = "th";
  char* arry_of_string[] = {str1,str2,str3};
  int i;

  for (i=0;i<3;i++)
  {
    printf("%s\n",arry_of_string[i]);
  }
  return 0;

Feel free to mark the question answered if you are satisfied. 如果您满意,请随时标记回答的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM