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

7
  • try to study a bit more about arrays pw1.netcom.com/~tjensen/ptr/pointers.htm . Commented Mar 1, 2013 at 16:01
  • By the way - char word1 [6] ="fluffy" - "fluffy" is actually 7 characters. In C, a string is terminated with a \0 - which takes up one extra character. Commented Mar 1, 2013 at 16:03
  • 2
    const char* arr[] = { "literal", "string", "pointer", "array"};, and note the const. Commented Mar 1, 2013 at 16:03
  • &words[0] Er...you don't seem to have a variable words at that point. Did you make an error copying your code or leave out something? Commented Mar 1, 2013 at 16:04
  • @ArjunShankar Oh yeah I forgot about \0, thanks Commented Mar 1, 2013 at 16:06

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

12

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

Comments

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

Comments

7

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.