0

I'm trying to make a function check that first turns char *word to lowercase, then compares it to dictionary words stored in a linked list. I saw this post with similar title, but the issue seems different.

I'm getting the following error:

dictionary.c:171:23: error: variable 'temp' is uninitialized when used within its own initialization

And I know it's likely because I'm using a pointer for a char variable temp, while also using temp in temp's own initialization. I've tried a number of different things such as initializing temp with another name so I'm not using the same variable in its initialization. I keep getting errors. I'm quite new to C and would love some understanding on this.

my function is below:

// Returns true if word is in dictionary else false
//watch linked list vid
bool check(const char *word)
{
    // TODO
    printf("the word is %s", word);
    //need to use case
    node *cursor = head;
    while(cursor != NULL)
    {

      //this will point to previous places in linked list
      cursor = cursor -> next;

      //make it lowercase
    //   if (isalpha(word) )
    //   {
    //      word = tolower(word);
    //      printf ("\n Lowercase of Entered character is %c", Ch);
    //   }
    //   char *temp = strdup(string); // make a copy


char *temp  = strcpy (temp, word); // strlwr(word); //strdup(word); // make a copy

// adjust copy to lowercase //https://stackoverflow.com/questions/32063131/how-to-convert-constant-char-pointer-to-lower-case-in-c
unsigned char *tptr = (unsigned char *)temp;
while(*tptr) {
    *tptr = tolower(*tptr);
    tptr++;
}
//do things
// release copy
//free(temp);

        //iterate through the char[]
        int isEqual = 1;
        int i = 0;
        int j = 0;
        while(temp[i] != '\0' || cursor->word[j] != '\0')
        {
            if(temp[i] != cursor->word[j])
            {
            isEqual = 0;
            break;
            }
            i++;
            j++;
        }
        if(isEqual == 1)
        {
        printf("EVEN STEVEN! %s", temp);
        return true;
        }
    }
    return false;
}
Katie Melosto
  • 1,047
  • 2
  • 14
  • 35
  • `strdup` is the correct function to use. I see that it's commented out twice. Why is that? – user3386109 Dec 21 '19 at 07:08
  • @user3386109: there's a slight problem with `strdup()`: it is not described by the C11 Standard (it is described by POSIX which is used in many many implementations) – pmg Dec 21 '19 at 08:17
  • @pmg That is indeed a very very slight problem, since on any implementation that doesn't support `strdup`, it's trivial to provide your own `strdup`. And arguably better design to do so, rather than scattering copies of that functionality in the code. – user3386109 Dec 21 '19 at 08:35
  • If you do not have `strdup`, then simply use `size_t len = strlen(word); char *temp = malloc (len + 1); if (temp == NULL) { perror ("malloc-temp"); exit (EXIT_FAILURE); }; memcpy (temp, word, len+1);` – David C. Rankin Dec 21 '19 at 08:36

3 Answers3

0

The problem is that you use the pointer temp here within its own initialisation/declaration:

char *temp  = strcpy (temp, word); // strlwr(word); //strdup(word); // make a copy

temp in strcpy() is not declared before the use of this expression and does not point to a char array. It points to nowhere or better said to its own entity, which isn´t even declared at that moment in time, which is invalid.

By the way: There is absolutely no reason to use the same identifier several times at the same scope in a C code, nonetheless this is quite invalid. In C++ this is different, while you can use there namespaces and their respective keywords to use the same identifiers for different variables and/or with different values at the same scope.

I've tried a number of different things such as initializing temp with another name so I'm not using the same variable in its initialization. I keep getting errors.

Can you provide the changed code and the errors you will get with it?

0

The line in question is

char *temp = strcpy(temp, word);

It is an initialization of a pointer, composed of two parts: the naming of the object

char *temp

and the value used to initialize it

             strcpy(temp, word);

In the 2nd part temp (the identifier has been declared but not initialized at this point) is not valid at this time. It is wrong to pass it to strcpy()


Note: the direct way to do what you want is:

char *temp = malloc(strlen(word) + 1); // allocate enough space
strcpy(temp, word); // no need to assign to temp
// use temp
free(temp);
pmg
  • 106,608
  • 13
  • 126
  • 198
0

Here

char *temp  = strcpy (temp, word); // strlwr(word); //strdup(word); // make a copy

you define temp as a pointer to char.
That is the part

char *temp;

This part

= strcpy (temp, word); // strlwr(word); //strdup(word)

initialises that new variable named temp, i.e. this is the initialisation.

The initialisation calls the function strcpy, giving two parameters, temp and word.
So your code uses temp to initialise temp.
This is what the error message is telling you. And rightly so, because strcpy returns a copy of the destination pointer on success, but only on success. And it cannot succeed with a pointer that is not yet pointing to useable memory. In the worst case it might at first seem to have worked and then cause trouble later.

In order to fix, first initialise temp

char* temp = NULL:

Then get the desired value into it. That could be done with the function you obviously already discovered strdup, but that is experimental or posix. Maybe that is why you did not use. (https://en.cppreference.com/w/c/experimental/dynamic/strdup)

In order to later use strcpy() you need first to get some memory to use for that operation. If you do not have some normal variable to receive the copy of the string, then you can use malloc(), with a suitable size you first need to find out.

By the way, strlwr() is not a standard fucntion, so I am with you on not using it. (undefined reference to `strlwr')

Yunnosch
  • 26,130
  • 9
  • 42
  • 54