Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing argument makes pointer from integer without a cast

Tags:

c

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;
}
like image 920
DanBennett Avatar asked Feb 13 '23 03:02

DanBennett


1 Answers

Vowels is a char*, *Vowels is just a char, 'a'. chars get automatically promoted to integers, which your compiler is allowing to be implicitly converted to a pointer. However the pointer value will not be Vowels, it will be the address equal to the integer encoding of the character 'a', 0x61 almost universally.

Just pass Vowels to your function.

like image 80
John Avatar answered Feb 15 '23 12:02

John