Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

c

string

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

like image 954
Wobblester Avatar asked Mar 01 '13 15:03

Wobblester


People also ask

How do you create an array of strings in C?

It's a two dimensional character array! Here is how an array of C string can be initialized: #define NUMBER_OF_STRING 4 #define MAX_STRING_SIZE 40 char arr[NUMBER_OF_STRING][MAX_STRING_SIZE] = { "array of c string", "is fun to use", "make sure to properly", "tell the array size" };

How do you copy an array of strings?

Using the inbuilt function strcpy() from string. h header file to copy one string to the other. strcpy() accepts a pointer to the destination array and source array as a parameter and after copying it returns a pointer to the destination string.

How do you create an array of strings?

To create a string array, you can concatenate string scalars using square brackets, just as you can concatenate numbers into a numeric array. You also can convert variables of different data types into string arrays using the string function, described below.

Can you copy strings in C?

strcpy can be used to copy one string to another. Remember that C strings are character arrays. You must pass character array, or pointer to character array to this function where string will be copied. The destination character array is the first parameter to strcpy .


2 Answers

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));
like image 154
Jay Avatar answered Oct 29 '22 21:10

Jay


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";
like image 29
Some programmer dude Avatar answered Oct 29 '22 22:10

Some programmer dude