-3

I'm trying to copy the contents of a C++ (.cpp file) to a C (.c file) code because i need to convert it. Most of the errors I got are now fixed, but there is one remaining.

Here is the code — I have removed all the things that don't relate with the issue:

/*Prototype*/
void breakTime(time_t time, tmElements_t tm);
/*Creation*/
static tmElements_t tm;
/*Functions*/
void breakTime(time_t timeInput,tmElements_t &tm){
    tm.Second = 10;
}

The compiler gives me:

expected ';' , ',' or ')'  before '&' token

I tried editing the function and replace &tm by just tm. It does compile, but the problem is the values that are assigned inside the function to tm. Members are not transfered to the outside of the function (which would be the idea).

Any advice how to solve this issues?

rocambille
  • 15,398
  • 12
  • 50
  • 68
  • what compiler are you using? – user2736738 Dec 31 '17 at 09:17
  • GCC (its for microprocessors) – user2921077 Dec 31 '17 at 09:21
  • Have you tried to find what `tmElements_t &tm` stands for? You could then be able to figure out similar C concept to make it work. Hint: http://en.cppreference.com/w/cpp/language/reference – Zdeněk Jelínek Dec 31 '17 at 09:22
  • @user2921077.: But then in `C` how is that `&` coming at that place? Isn't it? `C` doesn't have any reference like `C++`! – user2736738 Dec 31 '17 at 09:22
  • 1
    C doesn’t have references; C++ does. You’ll have to convert the reference to a pointer, and then change calls to the function too. And use appropriate pointer notation in the function of course. – Jonathan Leffler Dec 31 '17 at 09:25
  • @coderredoc as i said, i convertet from c++ to c, i was not sure what consequences it would bring. Its a library which i uses for many years, but now that i need it in C, i wanted to keep it and convert ist. It now works with the answer below.. – user2921077 Dec 31 '17 at 09:45
  • BTW, you'll better instead keep the C++ code, and call it from C code. – Basile Starynkevitch Dec 31 '17 at 09:56
  • @BasileStarynkevitch this does not work, since the compiler cannot compile C and C++ in the same project, you only can have full c++ or full c. Which results in the same - you have to edit libraries, and i choose the way of the lesser work (converting only 10 libraries, instead of like 50) – user2921077 Dec 31 '17 at 13:02
  • 1
    @user2921077: No, you can compile a C++ translation unit and another C translation unit and link them together. You'll use `extern "C"` in your C++ code to declare your C functions.This is very common (and most C++ standard library implementations are built that way). See [this answer](https://stackoverflow.com/a/48030464/841108) for more – Basile Starynkevitch Dec 31 '17 at 13:20
  • @user2921077: actually, on my Linux desktop, *every* C++ implementation is built above the `libc.so` standard library, and you can call C code from C++ code. Of course, the compiler processes translation units individually (compiled into [object files](https://en.wikipedia.org/wiki/Object_file)) and the linker is "mixing" them. Many C++ libraries (e.g. those from [Qt](http://qt.io/), etc....) are using C libraries (e.g. `libX11`) so calling C code from C++ is *extremely common*. – Basile Starynkevitch Dec 31 '17 at 13:24
  • @user2921077: ... and calling C++ code from C code (or libraries) is also possible (but requires some care). For example, [libgccjit](https://gcc.gnu.org/onlinedocs/jit/) is a library, mostly coded in C++ internally, which provides a C API and can be used from programs coded in C. You might find other examples (even if they are not so common). The point is that C and C++ [ABI](https://en.wikipedia.org/wiki/Application_binary_interface)s are similar, and with care, can be used together. However, C++ exceptions make that a bit complicated (so some care is needed). – Basile Starynkevitch Dec 31 '17 at 13:30
  • @user2921077: ... so on most implementations (and all the ones I know about) **you can use C and C++ in the same project** (with [GCC](http://gcc.gnu.org/), you'll better link with `g++` in such case), at least with enough care (need to understand ABI differences). – Basile Starynkevitch Dec 31 '17 at 13:35
  • @BasileStarynkevitch yes it might be possible, but alot of people have the same problem and have not found a solution yet, the main problem is another i think. Your idea might be true for computer systems, but microprocessors don't run on C or C++, they use their very own instruction set, because of that, at some point all the c/c++ stuff has to be translated to the instruction set of that controller. I think its that point where C and C++ causes a problem when used together. The file that comes out of compile is either *.elf or *.bin and then downloaded to the processor. – user2921077 Dec 31 '17 at 16:14
  • No, this is a confusion. As I said, you need to care about ABI, and indeed the ABI of C++ is quite complex (in the details). This is why coding a kernel in C++ is difficult, but not impossible. See [OSDEV C++](http://wiki.osdev.org/C%2B%2B) – Basile Starynkevitch Dec 31 '17 at 16:24

2 Answers2

4

The & symbol is used here to déclare tm as a reference, but C doesn’t have references. You have to remove references for pointers:

void breakTime(time_t timeInput, tm_Elements_t* tm) {
    tm->Second = 10;
}

Your calls to breakTime must also be converted. You should have code like this:

time_t timeInput;
tm_Elements_t tm;
breakTime(timeInput, tm);

Which must become:

time_t timeInput;
tm_Elements_t tm;
breakTime(timeInput, &tm);

Note the use of & here has a different meaning: it is used to take the address of tm which will be passed as a pointer to breakTime.

rocambille
  • 15,398
  • 12
  • 50
  • 68
0

Just replace by a pointer:

void breakTime(time_t timeInput, tmElements_t *tm) {
  tm->Second = 10;
}
Stargateur
  • 24,473
  • 8
  • 65
  • 91