如何在C中创建一个字符串数组?(How to create an array of strings in C? [duplicate])

编程入门 行业动态 更新时间:2024-10-23 23:34:04
如何在C中创建一个字符串数组?(How to create an array of strings in C? [duplicate])

这个问题在这里已经有了答案:

我如何在C中创建一个字符串数组? 14个答案

我从一本书教自己C,并试图创造一个纵横字谜。 我需要制作一串字符串,但仍然遇到问题。 另外,我不太了解阵列...

这是代码的一部分:

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*/

但我不断收到消息:

crossword.c:20:16: warning: assignment makes integer from pointer without a cast [enabled by default]

不知道是什么问题...我试图查找如何创建一个字符串数组,但没有运气

任何帮助都感激不尽,

山姆

This question already has an answer here:

How do I create an array of strings in C? 14 answers

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...

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

最满意答案

words_array[0]=word1;

word_array[0]是一个char ,而word1是一个char * 。 你的角色无法持有地址。

一串字符串可能看起来像这样:

char array[NUMBER_STRINGS][STRING_MAX_SIZE];

如果你想要一个指向你的字符串的指针数组:

char *array[NUMBER_STRINGS];

接着:

array[0] = word1; array[1] = word2; array[2] = word3;

也许你应该读这个 。

words_array[0]=word1;

word_array[0] is a char, whereas word1 is a char *. Your character is not able to hold an address.

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.

更多推荐

本文发布于:2023-08-06 09:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   创建一个   如何在   strings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!