我该如何解决这个问题code创建一个字符串数组? [英] How do I fix this code to create an array of strings?

查看:122
本文介绍了我该如何解决这个问题code创建一个字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个字符串数组。这里是code:

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;
}

这是行不通的行:

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

我如何改正?

推荐答案

如果你想创建一个数组的字符串的,你缺少一个星号,并终止零:

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};

当您使用字符X [] =...构造,您的字符串的内容(包括终止零)复制到内存你可以写,产生相同的效果字符x [] = {'','',......'\\ 0'} 结构。

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.

这篇关于我该如何解决这个问题code创建一个字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆