I am trying out arrays of strings in C. I have a dictionary array of strings, that I add words to and then print out the array to see if it worked. The output works, as i think it should, printing the words in the array. But I get a number of warnings that I am unable to fix.
// 20 word dictionary
#define ROWS 20
#define WORD_LENGTH 10
char dictionary[ROWS][WORD_LENGTH];
void add_word(char **dict, int index, char *word) {
dict[index] = word;
}
char *get_word(char **dict, int index) {
return dict[index];
}
void print_dictionary(char **dict) {
int i;
for (i = 0; i < 20; i++) {
printf("%d: %s\n", i, get_word(dict, i));
}
}
void test_dictionary() {
add_word(dictionary, 0, "lorem");
add_word(dictionary, 1, "ipsum");
print_dictionary(dictionary);
}
int main() {
test_dictionary();
}
The output of compiling this is,
p5.c: In function ‘test_dictionary’:
p5.c:54:2: warning: passing argument 1 of ‘add_word’ from incompatible pointer type [enabled by default]
p5.c:38:6: note: expected ‘char **’ but argument is of type ‘char (*)[10]’
p5.c:55:2: warning: passing argument 1 of ‘add_word’ from incompatible pointer type [enabled by default]
p5.c:38:6: note: expected ‘char **’ but argument is of type ‘char (*)[10]’
p5.c:57:2: warning: passing argument 1 of ‘print_dictionary’ from incompatible pointer type [enabled by default]
p5.c:46:6: note: expected ‘char **’ but argument is of type ‘char (*)[10]’
I tried changing **dict to dict[ROWS][WORD_LENGTH] without much difference. Can you guys please explain how to declare this dictionary parameter. Thanks.
Edit: My compiler flags are, CFLAGS = -Wall -g.
Edit2: Changed the declarations to,
void add_word(char dict[][WORD_LENGTH], int index, char *word) {
dict[index] = word;
}
char *get_word(char dict[][WORD_LENGTH], int index) {
return dict[index];
}
void print_dictionary(char dict[][WORD_LENGTH]) {
int i;
for (i = 0; i < 20; i++) {
printf("%d: %s\n", i, get_word(dict, i));
}
}
This gives a compilation error,
p5.c: In function ‘add_word’:
p5.c:42:14: error: incompatible types when assigning to type ‘char[10]’ from type ‘char *’
make[1]: *** [p5] Error 1
Thank you for all your help.
Ah! Figured it out!. Since it's a pointer, I need to use strcpy as suggested by @Jack.
void add_word(char dict[][WORD_LENGTH], int index, char *word) {
/*dict[index] = word;*/
strcpy(dict[index], word);
}
Thanks everyone!
Root Cause:
Arrays are not pointers!
Note that there is no implicit conversion from dict[][]
to dict **
and hence the error. When you pass a array to a function, it decays as the pointer to its first element.In case of two dimensional array the first element is an array of single dimenion itself and so you need a pointer to an array and not an double pointer.
Soultion:
You need to modify your function prototype to match the type you are passing.
void add_word(char dict[][WORD_LENGTH], int index, char *word);
void print_dictionary(char dict[][WORD_LENGTH]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With