-1

The reproduceable error code is:

#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//#include <cstring>

//functions

#define true 1
#define false 0



#ifdef _MSC_VER
// define POSIX function strndup if not available
char* strndup(const char* s, size_t n) {
    size_t len;
    for (len = 0; len < n && s[len]; len++)
        continue;
    char* ptr = malloc(len + 1);
    if (ptr) {
        memcpy(ptr, s, len);
        ptr[len] = '\0';
    }
    return ptr;
}
#endif

char** split(const char* str, const char* delimiters, int** a, int* size_of_a) {
    int i, count, len;
    char** final_result;
    const char* p;

    // phase 1: count the number of tokens
    p = str + strspn(str, delimiters);
    for (count = 0; *p; count++) {
        p += strcspn(p, delimiters);
        p += strspn(p, delimiters);
    }

    // phase 2: allocate the arrays
    final_result = calloc(sizeof(*final_result), count + 1);
    if (a) {
        *a = calloc(sizeof(**a), count);
    }
    if (size_of_a) {
        *size_of_a = count;
    }

    // phase 3: copy the tokens
    p = str;
    for (i = 0; i < count; i++) {
        p += strspn(p, delimiters);    // skip the delimiters
        len = strcspn(p, delimiters);  // count the token length
        if (a) {
            (*a)[i] = len;
        }
        final_result[i] = strndup(p, len); // duplicate the token
        p += len;
    }
    final_result[count] = 0;
    return final_result;
}


#ifdef __cplusplus
}
#endif

It started to give error:

Severity    Code    Description Project File    Line    Suppression State
Error   C2440   'initializing': cannot convert from 'void *' to 'char *'    example_win32_directx9  C:\Libraries\ImGui\imgui\examples\example_win32_directx9\Equation_simplifier.c  77  

How can this be fixed? I have set my compiler to C++14 and I am using visual studio 2019. I am using this in a non-main cpp file which is called in main cpp file. The main error I am getting from is malloc and calloc from what I have noticed. I am also getting error for getch().

13
  • Exact dupe of : How to call a C function in C++ without error? Commented Sep 17, 2022 at 8:55
  • 2
    extern "C" affects the calling convention, it doesn't make the code C. It is still C++, where conversion from void* doesn't happen automatically. Commented Sep 17, 2022 at 8:57
  • Please provide a minimal reproducible example. Commented Sep 17, 2022 at 8:58
  • The code you show is C++. Don't use extern "C" around the whole file. Don't` use malloc or calloc in C++. Commented Sep 17, 2022 at 8:58
  • 2
    You asked the old question just an hour ago. On a weekend. You have to be patient! Commented Sep 17, 2022 at 8:59

1 Answer 1

0

It seems you want to write code in plain C and then have the split function callable from C++.

Then you first of all need to make sure to build the source as plain C, which includes making sure the source file have a .c suffix, and not contain any C++ code (like the extern "C" part, or C++ header files like <cstring>).

Once the file is built as plain C you create a header file, which use conditional compilation to use extern "C", for example one named my_split.h and looking something like this:

#ifndef MY_SPLIT_H
#define MY_SPLIT_H

#ifdef __cplusplus
extern "C" {
#endif

char** split(const char* str, const char* delimiters, int** a, int* size_of_a);

#ifdef __cplusplus
}
#endif

#endif // MY_SPLIT_H

Include that header file in your C++ code, and link with the object file generated from the C source file, and you should be able to use the split function.


As mentioned, include the header file in the C source file as well, to make sure the function declarations match.

Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't it be better to have the .h included in the .c as well to ensure the function declaration doesn't become mismatched during (re-)development? The .h you've written has guards to screen C++ from the C compilation... Just curious... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.