Function strchr

Following the examples of function pointers in the Wikipedia article with the title Function Pointer at https://en.wikipedia.org/wiki/Function_pointer

The following fails to compile on Windows 10 Pro 64-bit using MSVS 2017 with:

Error C2440 'initializing': cannot convert from 'overloaded-function' to 'char *(__cdecl *)(const char *,int)'
E0386 no instance of overloaded function "strchr" matches the required type FunctionPointers001

Anyone know why ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>  
#include <string.h> 

double cm_to_inches(double cm) {
	return cm / 2.54;
}

// "strchr" is part of the C string handling (i.e., no need for declaration)
// See https://en.wikipedia.org/wiki/C_string_handling#Functions

int main(void) { 

	double (*func1)(double) = cm_to_inches; 

	char * (*func2)(const char *, int) = strchr;     // ERROR THIS LINE  

	printf("%f %s", func1(15.0), func2("Wikipedia", 'p')); 

	/* prints "5.905512 pedia" */ 

	return 0; 

}

Last edited on
In C++, strchr is overloaded. (See http://en.cppreference.com/w/cpp/string/byte/strchr ).
If you are writing a C program, use a C compiler.
Last edited on
I am not writing a C program, I am wondering why the program from Wikipedia does not compile.
SOLVED: In case anyone else runs into similar errors, or this exact problem, the correct line to make the code work is:

char * (*func2)( char *, int ) = strchr; // LINE NOW COMPILES and program works

But, I assume this is bad practice and another method for doing this is better.

Another function (xstrchr) to perform the same this as strchr is:

1
2
3
4
5
6
7
8
9
10
11
12
char* xstrchr( char *source, int c )
{ 
    while( *source ) 
    {
        if( *source == c ) 
        { 
            break; 
        } 
        source++; 
    }
    return source; 
}

But, again I assume this is bad practice and another method for doing this is better.

Last edited on
strchr has two signatures.
1
2
const char * strchr ( const char * str, int character );
      char * strchr (       char * str, int character );

You're trying to mix the signatures.
Your func2 pointer takes a const char * as the first argument, therefore it must return a const char *. Your prototype says it returns a char *. That doesn't match either of the two signatures.

The Wikipedia code is C code. As mbozzi said, if you''re going to compile a C program, use a C compiler.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Like I said, I am not writing C code. I was wondering why the code did not compile.
vtee2014 wrote:
char * (*func2)( char *, int ) = strchr; // LINE NOW COMPILES and program works
But, I assume this is bad practice and another method for doing this is better.

The better minimal change to turn that C program into a valid C++ program is to add, rather than remove, const:
const char * (*func2)(const char *, int) = strchr;
it's still going to be an ugly little program, like most C ported to C++ with minimal change.
Last edited on
Registered users can post here. Sign in or register to post.