2

I'm in a basic C programming course and I'm trying to create a hangman game. I've been stuck with a problem for the last three hours and I'm not getting any wiser.

Basically, I've created a function which reads a random line from a text file and then copies it to a string. Afterwards, I want to copy that string to another string outside off the function. This is because the main game is supposed to be completely built with functions.

This is the function that reads a random word from the text file and copies it to a string:

char datorns_val()
{
  char ordlista[20];
  char valt_ord[20];
  int raknare = 0;


  srand(time(NULL));
  random = rand()%10+0;          

  ptr_file =fopen("hangman.txt","r");
  if (!ptr_file)
  {
    return 1;
  }

  while (fgets(ordlista,20, ptr_file)!=NULL)
  {
    raknare++;
    if (raknare == random)
      strcpy(valt_ord, ordlista);
  }

  return valt_ord;
}

After this is done, I want to copy the word located in valt_ord to another string, and that's when I'm unsure about what to do:

char word[20];
strcpy(word,datorns_val());

I'm getting two errors that says:

Invalid conversion from 'char' to 'const char*'

and

initializing argument 2 of 'char* strcpy(char*, const char*)'

Am I on the right track here with using strcpy() twice or am I completely lost? I tried my build without a function structure and simply typing out all the code on after another and it works, if a replace the second strcpy() with a simple char word = valt_ord.

Thanks, Jonathan

(Sorry if my code is hard to understand, I'm swedish and my second language is English)

jww
  • 97,681
  • 90
  • 411
  • 885
JonteG
  • 23
  • 4
  • You are returning an char array that is on the stack, so this won't work. You need to return a copy of it on the heap, that you allocate with `malloc`. – toasted_flakes Jan 06 '14 at 09:05

3 Answers3

2

Currently you're returning a character, which is not of much use, since you need a string that will outlive the function which creates it. You should return a dynamically allocated string (using a pointer) for this.

 char* datorns_val()
 {
      // ... your current code
      char *ret_str = malloc(20);
      strcpy(ret_str, valt_ord);
      return ret_str;
 }

At the end where you use it, you should free it when done.

 char *result = datorns_val();
 // use result
 free(result);
 result = NULL;

Alternatively, if you're sure that the function which is calling the datorns_val is the only one which is going to use the result, then I'd recommend something else which doesn't involve dynamic memory alloc/decalloc (malloc/free). Pass the string to be loaded to datorns_val.

int datorns_val(char (*str_ptr)[20])   // pointer to an array of 20 chars
{
    // use str_ptr after dereferencing it to get back the char array
    // say you want to copy "abc" to it
    strcpy(*str_ptr, "abc");
    return 0;    // to denote success, you may return -1 for failure
}

// caller's end
char result[20] = "";
int success = datorns_val(&result);   // pass the array by reference

Read more about arrays and pointers to know more about them.

legends2k
  • 31,634
  • 25
  • 118
  • 222
  • Thank you legends, this solved the problem! I don't think dynamic memory allocation is necessary, since we haven't gotten to that part in the course yet. I do think I need to read up on pointers and arrays since the mistake I made feels a bit stupid. – JonteG Jan 06 '14 at 10:13
  • Welcome! Arrays and pointers are stumbling blocks when learning _C_. It becomes better when you read [the right books](http://stackoverflow.com/q/562303/183120) and practise a lot. Good luck :) – legends2k Jan 06 '14 at 10:31
0

Your function datorns_val is declared to return char while in fact it returns valt_ord that is of type char[]. Also there is a way bigger problem valt_ord is a local variable so even if you change the declaration the code will not work. You will need to allocate valt_ord dynamically(using malloc) or to pass it as function argument.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • You had to say "pass its adress as function argument" else it also wouldn't work. – dhein Jan 06 '14 at 08:51
  • @Zaibis It depends on what the author wants to do. My idea is to modify the array in the function, not allocate it and avoid using dynamic memory in this manner. – Ivaylo Strandjev Jan 06 '14 at 08:55
0

argument 2 of strcpy needed string but your function returning char.

man strcpy.

you trying to return local variable, it will be destroyed when you move out of function because it stores the data in stack. Use malloc

see this SO question for further clarification

Community
  • 1
  • 1
sujin
  • 2,813
  • 2
  • 21
  • 33