Doin' the Bull Dance, Feelin' the Flow... wrote:

> I assume that there are bound to be an abundance of C programmers out there,
> and I have a question on something that I think should work but isn't for
> some reason...  I'm pretty much a self-taught beginner, so I suppose I might
> have missed something...
>
> I've included the struct's definition that is being used, and the function
> that is having problems....I have isolated it to one line that is being
> nasty.  There is a comment in the code that shows where it is...
>
> If anyone could help, I would GREATLY appreciate it....thanks!!!!
>
> -Evan-
>
> typedef struct word{
>     int size;
>     char *WordGuessed;
>     char *WordToGuess;
> }Word;
>
> Word* init_word(FILE * in){
>
>  Word * readword;
>  char word_array[BUFF_LENGTH+1], temp_array[BUFF_LENGTH+1];
>  int word_array_length, inc = 0;
>  char dashes = '_';
>
>  fscanf(in, "%s", word_array);
>
>  word_array_length = 1 + strlen(word_array);
>
> /* Problem is below!!!  readword->WordToGuess has nothing in it, and this
> line causes some sort of exception that crashes the program...I have no idea
> why.  I tried casting the malloc as (char *) in case the fact that it is
> returning a void pointer has something to do with it...that doesn't change
> anything.  Help!  Please!*/
>

readword is an uninitialized pointer.Use

readword=malloc(sizeof(*readword));just here.

>  readword->WordToGuess = malloc(sizeof(char) * word_array_length);
>
> strcpy (readword->WordToGuess, word_array);
>
>  readword->size = 1 + strlen(readword->WordToGuess);
>  printf("The word to guess is %d letters long.\n\n", readword->size - 1);
>
>  for (inc = 0; inc <= word_array_length; inc++)
>   temp_array[inc] = dashes;
>
>  printf("%s\n\n", temp_array);
>
>  readword->WordGuessed = malloc(sizeof(char) * word_array_length);
>  strcpy(readword->WordGuessed, temp_array);
>
>  return readword;
>
> }


Reply via email to