[SOLVED] Warning message when attempting to bsearch()

Programming languages, Coding, executables, and scripting.
Post Reply
Message
Author
Caitlin
Posts: 305
Joined: 2012-05-24 07:32

[SOLVED] Warning message when attempting to bsearch()

#1 Post by Caitlin »

This is driving me crazy. I keep getting compile time warning message Suspicious pointer conversion in function fillbox. The line of code this is happening in is:

Code: Select all

if ( !bsearch(bigwords[5] + 1, shorties,
 sizeof(shorties) / sizeof(shorties[0]),
 sizeof(shorties[0]), strcmp))
The header and selected source lines of function fillbox() are:

Code: Select all

char* fillbox(struct ELEMENT*, char*);

char* fillbox(struct ELEMENT* taskwalk, char* floater)
 {
 const static char shorties[11][5] = {"a", "an", "at",
 "by", "go", "meet", "of", "see", "the", "to", "with"};
 char bigwords[6][36];
if ( !bsearch(bigwords[5] + 1, shorties,
 sizeof(shorties) / sizeof(shorties[0]),
 sizeof(shorties[0]), strcmp))
{bigwords[5][0] = wordsize;                               
{strcpy(bigwords[k], bigwords[k + 1]);};};
 return(g.workarea + 1);};
For reference, the headers for bsearch() [from stdlib.h] and strcmp() [from string.h] are:

Code: Select all

void   *_Cdecl bsearch(const void *key, const void *base, 
 size_t nelem, size_t width,
 int _Cdecl (*fcmp)(const void *, const void *));

int	 _Cdecl strcmp	(const char *s1, const char *s2);
I tried rearranging my code, typecasting the parameters in my code to match the header of bsearch(), and other things but nothing seems to work.

Any ideas?

Caitlin
Last edited by Caitlin on 2021-05-10 11:38, edited 1 time in total.

trinidad
Posts: 190
Joined: 2016-08-04 14:58
Been thanked: 2 times

Re: Warning message when attempting to bsearch()

#2 Post by trinidad »

You can't believe your eyes if your imagination is out of focus.

reinob
Posts: 935
Joined: 2014-06-30 11:42

Re: Warning message when attempting to bsearch()

#3 Post by reinob »

Can you post a working example of the code?

We don't know what a struct "ELEMENT" is, nor what "wordsize".
"k" and "g" (incl. "g.workarea") are also not defined.

And you have a very weird use of ; and { }, as well as formatting in general (but that should not be the problem, it just makes it very hard to debug).

reinob
Posts: 935
Joined: 2014-06-30 11:42

Re: Warning message when attempting to bsearch()

#4 Post by reinob »

Oh, and post the exact warning. Normally compilers (at least gcc and clang) helpfully show the line number where the offending code is..

Caitlin
Posts: 305
Joined: 2012-05-24 07:32

Re: Warning message when attempting to bsearch()

#5 Post by Caitlin »

The exact error message is Warning mtms9.c 237: Suspicious pointer conversion in function fillbox. In line 237 is the call to bsearch().

Does it matter what ELEMENT, wordsize, k, and g.workarea are? The statement with the problem does not reference any of these.

And my use of ; { } is NOT weird. It's an improvement over the nonsense I usually see. (Although the indenting is strange because I removed a lot of the irrelevant lines of code.)

Caitlin

reinob
Posts: 935
Joined: 2014-06-30 11:42

Re: Warning message when attempting to bsearch()

#6 Post by reinob »

strcmp expects (const char *) arguments, while bsearch requires a function taking (const void *).

If you replace strcmp with (int(*)(const void *, const void *)) strcmp, then at least you should get rid of a warning you should be getting about incompatible pointer types.

Whether that's the "suspicious pointer conversion" or not I cannot tell.
And no, you don't add a ; after a }

LE_746F6D617A7A69
Posts: 517
Joined: 2020-05-03 14:16

Re: Warning message when attempting to bsearch()

#7 Post by LE_746F6D617A7A69 »

1. "struct ELEMENT" does not define a type, this is a forward declaration/hidden type in C library interface.
2. "char bigwords[6][36]" defines an array of pointers to pointers to char -> and You're passing the "(bigwords[5] + 1)" to the function which expects an undefined "struct ELEMENT*" pointer - this is a mess, and the GCC is very "gently" pointing out that this is "suspicious" - but IMO, in this case a better warning message would be: "Apparently You don't know what You're doing" ;)
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

Caitlin
Posts: 305
Joined: 2012-05-24 07:32

Re: Warning message when attempting to bsearch()

#8 Post by Caitlin »

THANK YOU THANK YOU THANK YOU reinob. Typecasting the name of strcmp with (int(*)(const void *, const void *)) made the warning message go away. Also, I was under the impression that you ALWAYS needed a ; after a } but trying it I saw you NEVER need one (although it doesn't hurt).

FWIW, bigwords[5]+1 is a simple character string, no pointers.

(Also, I apologize for popping off the way I did. It was uncalled for.)

Caitlin (alias C38189A3938995) :-)

LE_746F6D617A7A69
Posts: 517
Joined: 2020-05-03 14:16

Re: [SOLVED] Warning message when attempting to bsearch()

#9 Post by LE_746F6D617A7A69 »

Caitlin wrote:Typecasting the name of strcmp with (int(*)(const void *, const void *)) made the warning message go away.
Yeah, cheating the compiler sometimes works, sometimes it does not. Sometimes changing the compiler type/version will cause a crash, but hey! the warning have dissappeared! Success!
Caitlin wrote:bigwords[5]+1 is a simple character string, no pointers.
Nope, it's not -> a "simple character string" means a pointer to an array of chars :roll:
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

Post Reply