I've read through several similar questions on Stack Overflow, but I've not been able to find one that helps me understand this warning in this case. I'm in my first week of trying to learn C though, so apologies if I've missed an obvious answer elsewhere on Stack Overflow through lack of understanding.
I get the following warning and note:
warning: passing argument 2 of ‘CheckIfIn’ makes pointer from integer without a cast [enabled by default]
if(CheckIfIn(letter, *Vowels) ){
^
note: expected ‘char *’ but argument is of type ‘char’
int CheckIfIn(char ch, char *checkstring) {
When trying to compile this code:
#include <stdio.h>
#include <string.h>
#define CharSize 1 // in case running on other systems
int CheckIfIn(char ch, char *checkstring) {
int string_len = sizeof(*checkstring) / CharSize;
int a = 0;
for(a = 0; a < string_len && checkstring[a] != '\0'; a++ ){
if (ch == checkstring[a]) {
return 1;
}
}
return 0;
}
// test function
int main(int argc, char *argv[]){
char letter = 'a';
char *Vowels = "aeiou";
if(CheckIfIn(letter, *Vowels) ){
printf("this is a vowel.\n");
}
return 0;
}
CheckIfIn(letter, *Vowels)should beCheckIfIn(letter, Vowels). Thensizeof(*checkstring) / CharSizeis not what you expect.#define CharSize 1 // in case running on other systems—charhas always a size of 1, even on non-8 bit systems. Thus,int string_len = sizeof(*checkstring) / CharSize;always evaluates to 1, on all systems. You need to pass the length of an array to a function as a separate parameter, or use a function likestrlenif your array has some sentinel value (like a 0-byte for a string):size_t string_len = strlen(checkstring);.const char*(A pointer to aconststring) and change the parameter type ofCheckIfInfromchar*toconst char*.