1

I am using visual studio 2013 with the visual studio 2010 compiler.

I am re-using some c code in my c++/CLI project and I am getting compile errors. All of the files are in the same project and all of the body files are not using pre-compiled headers

I am trying not to edit the re-used code, if possible...
Here is a sample of the code, note libFunc.h and .c are the re-used code and I am writing wrapper.cpp:

// libFunc.h
typedef struct
{
    double max;
    double var;
} myStruct;

void libFunc(char * filename, myStruct *(*arg)[]);
void readVals(char * filename, myStruct *(*arg)[]);
void assignVals(myStruct *arg, char vals[][1024]);

// libFunc.c
void libFunc(char * filename, myStruct *(*arg)[])
{
    readVals(filename, arg);
}

void readVals(char * filename, myStruct *(*arg)[])
{
    char vals[15][1024];
    char *line = (char *) malloc(1024);
    s = fopen(filename, "r");
    while(fgets(line, 1024, s) != NULL)
    {
        // parse tokens into vals
        assignVals((*arg)[index], vals);
    }
}

void assignVals(myStruct *arg, char vals[][1024])
{
    arg->max = atof(values[0]);
    arg->var = atof(values[1]);
}

// wrapper.cpp
#include "libFunc.h"

int wrapper()
{
    myStruct *myVar[1];
    char filename[512];

    strcpy(filename, "input.txt");

    libFunc(filename, &myVar);
}

I get the error:

cannot convert myStruct *(*)[1] to myStruct *(*)[].

I tried:

myStruct ** myVar = new myStruct*[1];
libFunc(&myVar);

This resulted in the error:

cannot convert myStruct *** to myStruct *(*)[].

How do I pass in something that it will accept?

The code does not have to be compiled as c++
Any solution to this issue must work in Visual Studio 2013 with Visual Studio 2010 compiler, and by "work" I mean that "Build Solution" has to build everything including these c files.

Chad
  • 111
  • 1
  • 12
  • 3
    I think you cannot call this function from C++ - only C allows pointer to array of unspecified bound. You'd need to redesign the function interface. – M.M Jan 08 '16 at 23:13
  • 5
    C or C++ - Your choice - Decide on one – Ed Heal Jan 08 '16 at 23:13
  • 1
    Possible duplicate of [What is array decaying?](http://stackoverflow.com/questions/1461432/what-is-array-decaying) – Chris Beck Jan 08 '16 at 23:15
  • You can write `libFunc((myStruct *(*)[])&a);` . It might work but I think it is formally undefined behaviour. – M.M Jan 08 '16 at 23:16
  • @ChrisBeck doesn't seem like a duplicate of that – M.M Jan 08 '16 at 23:16
  • Don*t add C tag for C++ questions! – too honest for this site Jan 08 '16 at 23:28
  • @Olaf, I added the C tag back in. This is a question about calling C from C++. Both tags apply. – David Hammen Jan 09 '16 at 07:38
  • @DavidHammen: I read "re-using C" different. OP seems to want to port the C code to C++ and compile as C++. This does not rectify adding the C tag. I*ll leave it until clarification by OP, but close-vote as too broad. – too honest for this site Jan 09 '16 at 09:04
  • @Olaf - The OP specifically wrote "I am trying not to edit the re-used code, if possible." That would mean calling C code from C++, and presumably compiling that C code using a C compiler. Note also that the OP specifically wrote that he is using two compilers. – David Hammen Jan 09 '16 at 09:17
  • @DavidHammen: That statement only means OP wants to compile the code unchanged as C++. This follows the false - yet often - claimed idea C is a subset of C++. He also does not state he uses two compilers, but the 2013 VS-IDE with the VS2010 compiler. He does not state he uses a C and a C++ compiler (whereas MSVC does not comply toe the C standard anyway) – too honest for this site Jan 09 '16 at 09:23
  • @Olaf -- How do you you know what that statement means? I'm assuming, unless specified otherwise, that the OP wants to use an existing library written in C. Because C and C++ are different languages, it's best to compile code written in C with a C compiler. There are no problems in calling that code from C++ if the C code in question is written entirely in the subset of C supported by C++. This is not one of those cases. – David Hammen Jan 09 '16 at 09:47

1 Answers1

2

Some people say that C is a subset of C++. This is incorrect. What is correct is that there exists a subset that is common to both languages. You are running into one of those aspects of C that is not supported in C++.

Since this is something that is not supported in C++, any answer is of dubious quality. That said, here's an answer of dubious quality: Simply cast your &myVar to a pointer to an array of pointers to myStruct:

libFunc((myStruct*(*)[])&myVar);

This compiles clean. Whether it "works", that's a different question. There's a comment in the question that makes me think this will not work:

void libFunc(myStruct *(*arg)[]){ // does stuff, sets arg

If that function truly does set arg (as opposed to sets contents of arg), that is something that is not supported at all in C++.


Aside: Why does that function take a pointer to an array of pointers to myStruct? You haven't shown the body of that code. I don't know if setting *arg is valid in C, but it certainly is not valid in C++.

David Hammen
  • 32,454
  • 9
  • 60
  • 108