Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi @ all!

I'm trying to convert a function to from C to C# and do not undestand what's going on here. The following definitions are given:

C++
int   **matrix;
char   *objects;


Later a conditions is set up in the following way:

C++
if( objects[ matrix[i][j] ] ) 
  <dosomething>


What is checked there? A char can be boolean in some way? Or is it C style NULL testing?

Thanks in advance

Rowan
Posted
Comments
Sergey Alexandrovich Kryukov 17-Sep-12 2:16am    
What do you mean by "convert", exactly?
--SA
RowanArkison 17-Sep-12 2:43am    
Convert in this case means, I have a c source code and I am stuck writing a C# implementation doing the same work as the function in C - so conversion is more a manual step :-D
Sergey Alexandrovich Kryukov 17-Sep-12 19:21pm    
I would call it "translation" :-)
Please see my previous comment, the one on your comment on my solution.
--SA

1 solution

Yes, this is kind of a way C goes instead of using Boolean, which does not exist. Everything which has binary representation filled with all zeros (null, integer 0, floating-point 0, character 0x00, etc) is considered false; and everything else — as true. More exactly, Boolean type itself does not even exist, but "if" is used as any type workes as Boolean. The definition or TRUE and FALSE is often used, but they is nothing more than some preprocessor (precompiler) macro definitions. So, there are millions of bugs stemmed from comparison like if (value == TRUE) instead of if (value) and a number of similar crazy problems. A nightmare… So, be very careful, especially interfacing with other languages.

—SA
 
Share this answer
 
v3
Comments
RowanArkison 17-Sep-12 2:41am    
Hi Sergey!

If I understand you right, this small sniplet just checks the object against zero. So this be a suitable representation in C#, right?

public enum objects
{
None,
TypeA,
[...]
}

if (objects[matrix[i][j]] == objects.None)
{
}

Regards Rowan
Sergey Alexandrovich Kryukov 17-Sep-12 19:20pm    
That's right. I don't understand what are you doing though. Are you just translating C to C#? If so, you should look more at the semantic of the code and first understand the semantic, and then translate it to C# code. If you try to formally translate from C to C#, you will have some big problem. First, many thing implemented in C at the level of application code is already done in .NET, at the level of framework. And then, if C code has bugs, you could reproduce them, something you don't want.

Good luck.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900