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)