How to create an array of strings in C? [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

This question already has an answer here:

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

回答1:

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.



回答2:

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 seven character. All strings have an extra character, '\0', that is used to tell the end of the string.

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


回答3:

If you need an array of strings. There are two ways:

1. Two Dimensional Array of characters

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

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


回答4:

You can also use malloc() to allocate memory manually:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!