1

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)
Sarah Cohen
  • 706
  • 1
  • 7
  • 11
  • 3
    `letter` is a local variable. It will always have a valid address. You probably meant `if(letter == NULL)` – tkausl Jul 14 '22 at 18:28
  • @tkausl THANK YOU! That solved that error. I thought that I saw a '&' used in an example I saw, but maybe I didn't or it was wrong. Would you like to post this comment as an answer to the question? – Sarah Cohen Jul 14 '22 at 18:35
  • `&letter` will always be not NULL as it an address of existing variable. – 0___________ Jul 14 '22 at 19:02

2 Answers2

3

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)

Colargolet
  • 31
  • 1
  • 2
2

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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335