Code conversion is giving error

Asked

Viewed 237 times

-1

I am trying to convert a code from C++ to C and doubts have arisen. The code starts like this:

#include<bits/stdc++.h> 
using namespace std; 

void function(int x, int y, int z,  char &teste, int *total) 

I thought of removing the #include, the namespace std and put only:

#include <limits.h> 
#include <stdio.h> 

Is that correct? It gives an error because of the &test. For what I should replace??

I’ve changed all the cout and cin. Further in the code, the compiler gives an error in a for, and says

for loop initial declarations are only allowed in C99 mode.

Why does he not accept the is normally? This occurs, for example, in these lines:

for (int i=1; i<n; i++) 
    senha[i][i] = 0;

2 answers

2

Conversion pure and simple like this is dangerous, you need to understand what you’re doing and all the implications. In this specific case demonstrated just switch to pointer that will solve the problem. You will break other parts and have to repair them. But again, it does not mean that everything will be as it wants. The biggest problem I see in what it wants to do is the naivety of action in something so simple. If you know C do in C without relying on conversion of anything else.

The wrong pain for is that you have to declare the variable at the beginning of the code, but it’s much better to configure the C compiler to accept C99 and not have to do this atrocity.

0


You will have several problems in the conversion. o char &teste says that the variable teste is being passed by reference, and C does not allow this sort of thing. So you should convert the function’s signature to:

void function(int x, int y, int z,  char *teste, int *total) 

And all access to teste should be replaced by (*teste) (access to pointer).

Regarding the error regarding the declaration of int i=1 within the for, the reason is that the standard C (C89) does not accept this type of declaration, only the C99 versions or higher. In the compiler gcc you can use the flag -std=c99 to specify the use of this version.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.