Question

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)

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top