Error In Conversion C++ Code To C

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[// C++ Implementation to find the 
02
// XOR of the two Binary Strings
03
 
04
#include<bits/stdc++.h>
05
using namespace std;
06
   
07
// Function to find the 
08
// XOR of the two Binary Strings
09
string xoring(string a, string b, int n){
10
string ans = "";
11
       
12
    // Loop to iterate over the
13
    // Binary Strings
14
    for (int i = 0; i < n; i++)
15
    {
16
        // If the Character matches
17
        if (a[i] == b[i])
18
            ans += "0";
19
        else
20
            ans += "1";
21
    }
22
    return ans;
23
}
24
   
25
// Driver Code
26
int main()
27
{
28
    string a = "1010";
29
    string b = "1101";
30
    int n = a.length();
31
    string c = xoring(a, b, n);
32
    cout << c << endl;
33
} ]


I converted the above code to C:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<stdio.h>
02
 
03
string xoring(string a, string b, int n)
04
 
05
string ans = "";
06
     
07
    for (int i = 0; i < n; i++)
08
    {
09
         
10
        if (a[i] == b[i])
11
            ans += "0";
12
        else
13
            ans += "1";
14
    }
15
    return ans;
16
 
17
   
18
// Driver Code
19
int main()
20
{
21
    string a = "1010";
22
    string b = "1101";
23
    int n = a.length();
24
    string c = xoring(a, b, n);
25
    printf("c");
26
}


but getting errors :

main.c:3:1: error: unknown type name ‘string’
string xoring(string a, string b, int n)

main.c:21:5: error: unknown type name ‘string’
string a[] = "1010";

main.c:23:13: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
main.c:24:5: error: unknown type name ‘string’
string c = xoring(a, b, n);

------------------
Hello, my name is Soman, nice to meet you.
https://giupviectheogio.com/giup-viec-theo-gio.html
Last edited on
Converting languages doesn't just magically happen.

C's library does not have a "string" type.
C only has null-terminated character arrays built-in.

e.g.
1
2
3
4
5
6
char a[] = "1010";
char b[] = "1101";

char c[5]; // allocate enough room to hold result + null terminator

xoring(a, b, c, 4); // something like this 


1
2
3
4
5
6
7
8
void xoring(char a[], char b[], char out[], int n)
{
    for (int i = 0; i < n; i++)
    {
        out[i] = / * ... */ ;
    }
    out[n] = '\0';
}


Also, whatever editor you're using is copying your line numbers... you should get it to not do that.
Last edited on
this begs a rewrite.
converting code ends up making a huge mess. Just do it from scratch and it will be more natural, and work better. Only convert code if there is no other alternative.
C has only primitive type (builtin type in compiler) , no user-defined type at all such as string, vector, array etc, all of which actually bulk of classes packed in a library

so you must use pointers to char type processes and manipulations, to accomplish it to achieve the same result
Last edited on
no user-defined type at all such as string

it has user defined types: struct and typedef are both available. Function pointes allow you to make 'methods' for structs, as well, in a crude fashion. Not needed here, as the c char array tools are sufficient.
Last edited on
> Error In Conversion C++ Code To C
Also know as

I found this code in language X, but I need it in language Y, because of #include "std_excuses.h".

A few seconds of google-fu later.
https://www.geeksforgeeks.org/xor-of-two-binary-strings/
Fine, until your tutor does the same.

Topic archived. No new replies allowed.