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

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

问题描述

我从一本书自学C和我试图创建一个纵横字谜。我需要一个字符串数组,但继续运行出现问题。另外,我不知道很多有关数组...

这是一块code的:

 字符字1 [6] =蓬松,WORD2 [5] =小,WORD3 [5] =兔子;炭words_array [3]; / *这是我的数组* /字符* first_slot =安培; words_array [0]; / *我做了一个指针字的第一个插槽* /words_array [0] =字词1; / *试图把这个词蓬松到阵列的拳头槽(20行)* /

不过,我不断收到消息:

  crossword.c:20:16:警告:赋值时将指针整数,未作施放[默认启用]

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

任何帮助将非常AP preciated,

山姆


解决方案

  words_array [0] =字1;


word_array [0] 字符,而字1 的char * 。你的性格是不是能够保持一个地址。

的字符串数组看起来像它:

 字符数组[NUMBER_STRINGS] [STRING_MAX_SIZE];

如果您更希望指针数组到你的字符串:

 的char *数组[NUMBER_STRINGS]

和则:

 数组[0] =字1;
阵列[1] =字词2;
数组[2] = WORD3;

也许你应该阅读这个

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

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

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