-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().

Himanshu
  • 21
  • 6
  • Exact dupe of : [How to call a C function in C++ without error?](https://stackoverflow.com/questions/73752937/how-to-call-a-c-function-in-c-without-error) – Jason Sep 17 '22 at 08: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. – BoP Sep 17 '22 at 08:57
  • Please provide a [mcve]. – Quimby Sep 17 '22 at 08:58
  • The code you show is C++. Don't use `extern "C"` around the whole file. Don't` use `malloc` or `calloc` in C++. – Some programmer dude Sep 17 '22 at 08:58
  • @JasonLiam No one was answering in previous question even when I fixed the details so I made new one. – Himanshu Sep 17 '22 at 08:58
  • @Quimby I have provided in beginning of question. – Himanshu Sep 17 '22 at 08:59
  • 2
    You asked the old question just an hour ago. On a weekend. You have to be patient! – Some programmer dude Sep 17 '22 at 08:59
  • @BoP How can I fix it? – Himanshu Sep 17 '22 at 08:59
  • @Himanshu Hopefully someone here will answer. – Jason Sep 17 '22 at 09:00
  • And if you want one function to be callable from C, use `extern "C"` ***only*** for that function. And write the code for the function using plain and good C++, using all the C++ standard functionality that's possible. – Some programmer dude Sep 17 '22 at 09:00
  • @Someprogrammerdude So I will have to rewrite the malloc and calloc c code in c++? – Himanshu Sep 17 '22 at 09:01
  • If you want to do the opposite, create the function in C and make it callable from C++, then build the code as plain C, using the C compiler (the file needs to have the `.c` suffix), and use `extern "C"` in a *header* file (when compiled by a C++ compiler). – Some programmer dude Sep 17 '22 at 09:03
  • OT: You got the parameters to `calloc()` reversed. Should not be an issue but to demonstrate that you are not really paying attention to the man page. – Fe2O3 Sep 17 '22 at 09:05

1 Answers1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    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... `:)` – Fe2O3 Sep 17 '22 at 09:14