C语言 如何在C中创建一个字符串数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15161774/
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
How to create an array of strings in C?
提问by Wobblester
I'm teaching myself C from a book and I am trying to create a crossword puzzle. I need to make an array of strings but keep running into problems. Also, I don't know much about array...
我正在从一本书中自学 C,我正在尝试创建一个填字游戏。我需要制作一个字符串数组,但一直遇到问题。另外,我对数组不太了解......
This is the piece of the code:
这是代码的一部分:
char word1 [6] ="fluffy", word2[5]="small",word3[5]="bunny";
char words_array[3]; /*This is my array*/
char *first_slot = &words_array[0]; /*I've made a pointer to the first slot of words*/
words_array[0]=word1; /*(line 20)Trying to put the word 'fluffy' into the fist slot of the array*/
But I keep getting the message:
但我不断收到消息:
crossword.c:20:16: warning: assignment makes integer from pointer without a cast [enabled by default]
Not sure what is the problem...I have tried to look up how to make an array of strings but with no luck
不知道是什么问题......我试图查找如何制作一个字符串数组但没有运气
Any help will be much appreciated,
任何帮助都感激不尽,
Sam
山姆
回答by md5
words_array[0]=word1;
words_array[0]=word1;
word_array[0]is a char, whereas word1is a char *. Your character is not able to hold an address.
word_array[0]是 a char,而word1是 a char *。您的角色无法保存地址。
An array of strings might look like it:
一个字符串数组可能看起来像这样:
char array[NUMBER_STRINGS][STRING_MAX_SIZE];
If you rather want an array of pointers to your strings:
如果您想要一个指向字符串的指针数组:
char *array[NUMBER_STRINGS];
And then:
进而:
array[0] = word1;
array[1] = word2;
array[2] = word3;
Maybe you should read this.
也许你应该阅读这个。
回答by Jay
If you need an array of strings. There are two ways:
如果你需要一个字符串数组。有两种方式:
1. Two Dimensional Array of characters
1.二维字符数组
In this case, you will have to know the size of your strings beforehand. It looks like below:
在这种情况下,您必须事先知道字符串的大小。它看起来像下面:
// This is an array for storing 10 strings,
// each of length up to 49 characters (excluding the null terminator).
char arr[10][50];
2. An array of character pointers
2. 字符指针数组
It looks like below:
它看起来像下面:
// In this case you have an array of 10 character pointers
// and you will have to allocate memory dynamically for each string.
char *arr[10];
// This allocates a memory for 50 characters.
// You'll need to allocate memory for each element of the array.
arr[1] = malloc(50 *sizeof(char));
回答by Some programmer dude
The declaration
声明
char words_array[3];
creates an array of three characters. You seem to want to declare an array of character pointers:
创建一个包含三个字符的数组。您似乎想声明一个字符指针数组:
char *words_array[3];
You have a more serious problem though. The declaration
不过你有一个更严重的问题。声明
char word1 [6] ="fluffy";
creates an array of six character, but you actually tell it to have sevencharacter. All strings have an extra character, '\0', that is used to tell the end of the string.
创建一个包含六个字符的数组,但您实际上告诉它有七个字符。所有字符串都有一个额外的字符 ,'\0'用于告诉字符串的结尾。
Either declare the array to be of size seven:
要么声明数组的大小为七:
char word1 [7] ="fluffy";
or leave the size out, and the compiler will figure it out by itself:
或者不考虑大小,编译器会自己计算出来:
char word1 [] ="fluffy";
回答by kamituel
You can also use malloc()to allocate memory manually:
您还可以使用malloc()手动分配内存:
int N = 3;
char **array = (char**) malloc((N+1)*sizeof(char*));
array[0] = "fluffy";
array[1] = "small";
array[2] = "bunny";
array[3] = 0;
If you don't know in advance (at coding time) how many strings will be in an array and how lengthy they'll be, this is a way to go. But you'll have to free the memory when it's not used anymore (call free()).
如果您事先(在编码时)不知道数组中有多少个字符串以及它们有多长,这是一种方法。但是当它不再使用时,你必须释放它(调用free())。

