11

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

Wobblester
  • 748
  • 4
  • 8
  • 18

4 Answers4

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

md5
  • 23,373
  • 3
  • 44
  • 93
10

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));
kamituel
  • 34,606
  • 6
  • 81
  • 98
Jay
  • 24,173
  • 25
  • 93
  • 141
9

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";
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
6

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

kamituel
  • 34,606
  • 6
  • 81
  • 98