-3

Getting error as void* cannot be assigned to an entity of type char*.

What should be done in order to resolve the error? The problem occurs with xmlpath and dllPath.

void fmuLoad()
{
    char* fmuPath;
    char tmpPath[1000]="W:\\Prajwal\\GM_FMU_EXTRACT\\";
    char* xmlPath;
    char* dllPath;
    const char *modelId;
    FMU fmu;
    fmuUnzip();

    // parse tmpPath\modelDescription.xml
    xmlPath = calloc(sizeof(char), strlen(tmpPath) + strlen(XML_FILE) + 1);
    sprintf(xmlPath, "%s%s", tmpPath, XML_FILE);
    fmu.modelDescription = parse(xmlPath);
    free(xmlPath);
    if (!fmu.modelDescription) {
        exit(EXIT_FAILURE);
    }
    //printf(fmu.modelDescription);

#ifdef FMI_COSIMULATION
    modelId = getAttributeValue((Element*)getCoSimulation(fmu.modelDescription),att_modelIdentifier);
//#else // FMI_MODEL_EXCHANGE
    //modelId = getAttributeValue((Element*)getModelExchange(fmu.modelDescription), att_modelIdentifier);
#endif

    // load the FMU dll
    dllPath = calloc(sizeof(char), strlen(tmpPath) + strlen(DLL_DIR) + strlen(modelId) +  strlen(".dll") + 1);
    sprintf(dllPath, "%s%s%s.dll", tmpPath, DLL_DIR, modelId);
    if (!loadDll(dllPath, &fmu)) {
        exit(EXIT_FAILURE);
    }
    //free(dllPath);
    //free(fmuPath);
    //free(tmpPath);
}
Burak
  • 2,251
  • 1
  • 16
  • 33
PrajwalBhat
  • 51
  • 2
  • 3
  • 10

3 Answers3

1

In C++, a cast is required to assign a void pointer.

xmlPath = (char*)calloc(sizeof(char), strlen(tmpPath) + strlen(XML_FILE) + 1);

Or, using C++ style:

xmlPath = static_cast<char*>( calloc(sizeof(char), strlen(tmpPath) + strlen(XML_FILE) + 1) );

Of course, one should really question why you are using old C library functions like calloc at all. If you are actually compiling a C program, try telling your compiler that it is C and not C++. Then the casting is not necessary.

paddy
  • 60,864
  • 6
  • 61
  • 103
1
static_cast<char*>(calloc(sizeof(char), strlen(tmpPath) + strlen(DLL_DIR) + strlen(modelId) +  strlen(".dll") + 1));

return type of calloc is void*. You must cast the result of calloc explicitly.

-1

I'm having the same problem. Then, I change my extension file from .cpp to .c. Finally, my code can run.

  • 2
    The assignment is allowed without a cast in C, but not in C++. These are different languages (and this is only one of the ways in which they differ). Switching languages, which is what you have done by changing the source file extension, is a pretty drastic means to resolve such an error, and often not a viable one. – John Bollinger Oct 21 '22 at 17:22
  • While I agree with @JohnBollinger, it seems like he himself removed [tag:c] from the original question. Considering the functions used in the code, it should have been compiled with C in the first place, not C++. This answer seems exceptionally valid, but it could use some explanation. – Burak Oct 23 '22 at 07:34