Reputation: 39
I'm very new to C.
I'm supposed to do a simple wordsearch puzzle, so for the "dictionary" I did:
char **dictionary = {"DOG", "ELEPHANT", "CAT", "ETC", ""};
But when I try to compile, I get a warning saying 'excess elements in scalar initializer' for every word in the dictionary. Is the char ** wrongly initialized? How should I do it?
EDIT: My functions recieve char **dictionary.
Thanks in advance.
Upvotes: 3
Views: 4264
Reputation: 1
//The best is to do like that
char **dictionary = new char*[] {(char*)"DOG", (char*)"ELEPHANT", (char*)"CAT", (char*)"ETC", (char*)""};
char* myword=new char[]{"my word"};
Upvotes: 0
Reputation: 212198
const char *data[] = {"DOG", "ELEPHANT", "CAT", "ETC", ""};
const char **dictionary = data;
Upvotes: 5
Reputation: 3511
const char **addresses = {"0xabcdefgh"};
produces only a warning
Incompatible pointer types initializing 'const char **' with an expression of type 'char [11]'
[clang -Wincompatible-pointer-types]
and it fails when accessing addresses[0]
.
Example:
/*
clang t.c && ./a.out
gcc t.c && ./a.out
*/
#include <stdio.h>
void printaddress0(const char **addresses) {
printf(addresses[0]);
}
int main() {
//const char **addresses = {"0xabcdefgh"}; // segmentation fault
const char *addresses[] = {"0xabcdefgh"};
printaddress0(addresses);
}
Upvotes: 0
Reputation: 4864
char **values = (char *[]){"a", "b", "c"};
or
const char *array[] = {
"First entry",
"Second entry",
"Third entry",
};
Upvotes: 2