Jay Mosk
Jay Mosk

Reputation: 337

passing argument 1 of 'strlen' makes pointer from integer without a cast

It is not clear why I get a warning of:

[Warning] passing argument 1 of 'strlen' makes pointer from integer without a cast [enabled by default] expected 'const char *' but argument is of type 'char'

on two of the 3 statements containing strlen() below.

Even when I attempted to cast *str it still gave the same warning. bfr is a character buffer. *str points to that char buffer after the call to gets(). If I use strlen(*str) I get a warning. If I use strlen(bfr) I do not. But *str should be the equivalent to bfr. Thus the confusion regarding the error.

Now in reality, strlen arg 1 is defined as strlen(const char *string). So I would have expected strlen(bfr) to also produce an error since bfr[] is a char string and not a const char either.

And where is the integer that is being made into a pointer?

I am using gcc under wXDev-C++.

void test(){
    FILE *fileID = fopen("somefile.txt","r");
    char *str, len;
    char bfr[16];

    str = fgets(bfr,16,fileID);      // str will be set equal to &bfr[0]
    len = strlen(*str);              // This gives a warning
    len = strlen((const char)*str);  // This gives a warning
    len = strlen(bfr);               // This does not give a warning
}

Upvotes: 3

Views: 13665

Answers (1)

Jay Mosk
Jay Mosk

Reputation: 337

Sometimes you just need to take a fresh look in the morning at a problem. I realized that strlen is looking for a pointer to a string and 'str' is defined as a pointer. So *str would be a pointer to a pointer. So the warning was correct. It should read len = strlen(s) not len = strlen(*s). And it is 'str' pointing to 'bfr' not *str;

Answered my own question.

Upvotes: 7

Related Questions