-1

Have looked for a similar answer but nothing I try works. Have an issue, I want to change the value of word by calling the void function init() but when I print the word it does not work.

Have spent way to many hours on this so any help would be appreciated.

int main(void)
{
   char word[MAX_WORD_LEN + 1];
   unsigned wrongGuesses = 0;
   int guessedLetters[ALPHABET_SIZE] = {
   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   };
   init(&word);
   printf("%s", word);
   displayWord(word, guessedLetters);
   guessLetter(word, guessedLetters);
   return EXIT_SUCCESS;
}

void init(char* word)
{
    int randValue;
    char* randWord;
    const char* words[NUM_WORDS] = {
      "array",      "auto",       "break",      "case",       "cast",
      "character",  "comment",    "compiler",   "constant",   "continue",
      "default",    "double",     "dynamic",    "else",       "enum",
      "expression", "extern",     "file",       "float",      "function",
      "goto",       "heap",       "identifier", "library",    "linker",
      "long",       "macro",      "operand",    "operator",   "pointer",
      "prototype",  "recursion",  "register",   "return",     "short",
      "signed",     "sizeof",     "stack",      "statement",  "static",
      "string",     "struct",     "switch",     "typedef",    "union",
     "unsigned",   "variable",   "void",       "volatile",   "while"
    };
    int seed;
    seed = (time(NULL));
    srand(seed);
    randValue = rand() % NUM_WORDS;
    randWord = words[randValue];
    printf("%s", randWord);
    *word = randWord;
}
TryinHard
  • 4,078
  • 3
  • 28
  • 54
  • Your main should have the following signature: `int main(int argc, char* argv[])`. Especially for C++ (which you've tagged this question as, so I assume there's a reason for that). – celticminstrel Jul 03 '15 at 03:25
  • Aside from the problem you have, this variation of the table of words makes it far easier to survive wayward maintenance without breaking anything: `const char *words[] = {"array", "auto",` ...`, "while"}; #define NUM_WORDS (sizeof (words)/sizeof (words[0]))` – wallyk Jul 03 '15 at 03:49
  • `word` already of type `char*` so you don't need to pass it to `init` as `&word` because it's wrong too!! – ThunderWiring Jul 03 '15 at 10:29

3 Answers3

3

Strings are not assignable in C.

You need to use strcpy() or memcpy() to copy the string

strcpy(word,randWord);
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • Of course! Can't say I am very fond of C. Thank you for answering my nooby question. – abndonurfriends Jul 03 '15 at 03:23
  • Also, consider changing `char* randWord;` to `char* randWord = NULL;` – WedaPashi Jul 03 '15 at 04:18
  • @abndonurfriends: If you're not very fond of C, and C++ is an option (you did tag it C++ after all), then pick up a beginners C++ book. With `std::string` you can in fact do `word = randword` and it just works.. – MSalters Jul 03 '15 at 07:31
2

Pay attention to compiler warnings.

word.c: At top level:
word.c:16:6: warning: conflicting types for ‘init’
 void init(char* word)
      ^
word.c:8:4: note: previous implicit declaration of ‘init’ was here
    init(&word);

You pass pointer to pointer to init().

And yeah, as @Gopi said, you can't copy strings by assigning.

JIghtuse
  • 866
  • 5
  • 11
1

Here are two different ways to fix this:

  1. pass init(word), and down in init say strcpy(word, randWord).

  2. Change word to const char *word, continue to call init(&word), change the definition of init to void init(const char ** word), and leave the assignment *word = randWord. (In this case you should declare words[] and randWord as const char *, also.)

Steve Summit
  • 45,437
  • 7
  • 70
  • 103