Home > Software engineering >  Why is there an error that the pointer is always null or true depending on the if statement wording?
Why is there an error that the pointer is always null or true depending on the if statement wording?

Time:07-15

I'm not very versed in C so working with pointer errors is not going well for me. Any help would be appreciated.

I have two versions of this piece of code and no matter what I've changed with the initializing or the if statement, there is a logic error. I'm definitely missing some basic information regarding pointers and C. Here are the two versions with their respective errors:

VERSION 1:

//check if every letter of the alphabet is present in the text string.
char alphabet[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char *letter;
char buf[strlen(codetext)   1];
strcpy(buf, codetext);
for(int i = 0; i < 26; i  )
{

    letter = strchr(buf, alphabet[i]);
    if(!&letter)
    {
        printf("Key must contain every alphabetic character. \n");
        return 1;
    }
}

ERROR 1:

error: address of 'letter' will always evaluate to 'true' [-Werror,-Wpointer-bool-conversion]
    if(!&letter)

VERSION 2:

//check if every letter of the alphabet is present in the text string.
char alphabet[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char *letter;
char buf[strlen(codetext)   1];
strcpy(buf, codetext);
for(int i = 0; i < 26; i  )
{

    letter = strchr(buf, alphabet[i]);
    if(&letter == NULL)
    {
        printf("Key must contain every alphabetic character. \n");
        return 1;
    }
}

ERROR 2:

error: comparison of address of 'letter' equal to a null pointer is always false [-Werror,-Wtautological-pointer-compare]
    if(&letter == NULL)

CodePudding user response:

letter is a pointer to a char. &letter is the address of this pointer, so it will never be false. If you want to check whether it is a NULL pointer you need to use: if (!letter) or if (letter == NULL)

CodePudding user response:

As the pointer letter is defined

char *letter;

then a pointer to it can not be a null pointer.

So these if statements

if(!&letter)
{
    printf("Key must contain every alphabetic character. \n");
    return 1;
}

and

if(&letter == NULL)
{
    printf("Key must contain every alphabetic character. \n");
    return 1;
}

where in fact the conditions are equivalent do not make a sense.

What you need is to write either as

if(!letter)
{
    printf("Key must contain every alphabetic character. \n");
    return 1;
}

or as

if( letter == NULL )
{
    printf("Key must contain every alphabetic character. \n");
    return 1;
}

The function strchr returns NULL if the specified character is not found. This value you assigned to the pointer letter.

letter = strchr(buf, alphabet[i]);

So you need to compare the value of the pointer with NULL.

Actually the declaration of the variable letter is redundant. You could just write

if ( !strchr(buf, alphabet[i]) )
{
    printf("Key must contain every alphabetic character. \n");
    return 1;
}
  • Related