Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:16:53+00:00 2026-06-16T00:16:53+00:00

I’m trying to compile a C codebase as C++, tweaking some of the includes.

  • 0

I’m trying to compile a C codebase as C++, tweaking some of the includes. It uses strchr() on unsigned char pointers, e.g.:

#include <string.h>
#include <stdio.h>

// Cannot modify this file with any non-C-isms, but let's say an include can
// be changed (although all things being equal I'd rather not)
#include <SomethingICanChange.h>

int main(int argc, char* argv[]) {
    unsigned char b = 'B';
    unsigned char const * str = "ABC";
    unsigned char const * pos = strchr(str, b);
    if (pos) {
        printf("position is %d\n", pos - str);
    }
    return 0;
}

That causes an error in C++ (for reasons explained elsewhere)…even with -fpermissive.

test.c: In function ‘int main(int, char**)’:
test.c:6:33: warning: invalid conversion from ‘const char*’ to ‘const unsigned char*’ [-fpermissive]
test.c:7:46: error: call of overloaded ‘strchr(const unsigned char*&, unsigned char&)’ is ambiguous
test.c:7:46: note: candidates are:
In file included from test.c:1:0:
/usr/include/string.h:215:14: note: char* strchr(char*, int) <near match>
/usr/include/string.h:215:14: note:   no known conversion for argument 1 from ‘const unsigned char*’ to ‘char*’
/usr/include/string.h:217:22: note: const char* strchr(const char*, int) <near match>
/usr/include/string.h:217:22: note:   no known conversion for argument 1 from ‘const unsigned char*’ to ‘const char*’

Usually when faced with this kind of thing I go “strchr, yuck, just get rid of it entirely”. But I don’t want to modify these files myself, and this is C code that is being kept very portable to rather old platforms. They wouldn’t be happy putting casts at callsites to appease C++…though if a “struchr” existed and was standard they’d probably use it.

I can also hack around it one way or another, like in SomethingICanChange.h I could add:

unsigned char const * strchr(unsigned char const * s, int c) {
    return (unsigned char const *)strchr((char const *)s, c);
}

That works with -fpermissive. But I wouldn’t be able to check that overload into the C codebase (well, without an #ifdef). Just wondering if anyone has a better idea, I will pitch adding struchr if that’s what people in this situation wind up doing (or whatever name is common if there’s a common one).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:16:54+00:00Added an answer on June 16, 2026 at 12:16 am

    Initial note: I am aware that this is also quite an ugly thing to do, but it might help you with a (hopefully) better idea!

    How about using a macro:

    #define strchr(s, c) ((unsigned char const *)strchr((char const *)s, c))
    

    You could include it as a compile flag (-D) from you Makefile/build script. I suppose you have a custom Makefile/build script for your C++ project, so you do not need to add this macro to the C code-base.

    To make it slightly better, you can use an additional file, that you include from your Makefile/build script, which adds these macros in a (more) structured way to CPPFLAGS

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.