Search code examples
javac++portingtypecasting-operator

Syntax for Structure and Pointers in C++ equivalent for Java


Here I have a part of my code. There are 2 simple structures which are later used in the .cpp Send() method.

    //In the header file I have

        #define P32 (unsigned int)
        #define P16 (unsigned short)
        #define P8 (unsigned char)

        struct nd {
            P8 p;
            P8 c;
            P16 l;
        };

        struct HELLO {
            P32 a1;
            P32 a2;
            P8  a3;
        };


    //In the .cpp I have

        void Send()
        {
            DWORD dw = nd_s + sizeof( HELLO);
            BYTE *HelloPac=new BYTE[dw];
            nd *HelloHr=(nd*)HelloPac; 
            HELLO* _Hello=(HELLO*)(HelloPac+nd_s);

            HelloHr->c=0x10;
            HelloHr->p=0x09;
            Hellohr->l=36;

            _HELLO->a1=6001;
            _HELLO->a2=0
            _HELLO->a3=120;

           //my own read write function
           streamReadWrite->Write(HelloPac, dw);
        }

I am writing the same code in Java (porting the code). I am confused since I haven't done much coding in Java and since Java has no pointers no structures no unsigned integers, I am not getting how the syntax will be for the above code. Here's what I have got, but t throws syntax errors:

public class abc {

    private static final int nd_s = 4; //hard-coded

    public class nd
    {
        public byte p;
        public byte c;
        public short l;
    }

    public class HELLO
    {        
        public int a1;
        public int a2;
        public byte a3;
    }

    private void Send()
    {    
        int dw = nd_s + 30;
        byte[] HelloPac = new byte[dw];
        nd HelloHr = (nd)HelloPac;
        HELLO _Hello = (HELLO)(HelloPac + nd_s);
    }
}

Where am I going wrong in typecasting?


Solution

  • A reference to a particular object acts as a pointer to that object in Java but it does not support pointer arithmetic unlike C/C++.

    HELLO hello1 = new HELLO();
    //Put values
    hello1.a1 = 5 
    
    //You should not use public variables, make them private and use getter/setter
    
    ....
    HELLO hello2 = new HELLO();
    //Put values
    hello2.a1 = 15 
    ....
    

    Here hello1 and hello2 are references to objects of type HELLO that we created.

    If you want to store references to all HELLO objects in a sequential order, you can store them in an Array of type HELLO.

    HELLO[] helloArray = new HELLO[10];
    helloArray [0] = hello1;
    helloArray [1] = hello2;
    

    You can browse the objects by iterating over the array.

    Or you could use linked list .

    public class HELLO
    {        
    
        HELLO nextHallo; // Store reference to next hello object.
        public int a1;
        public int a2;
        public byte a3;
    }
    HELLO hello1 = new HELLO();
    hello1.a1 = 5 (Ideally you should not have public variables)
    ....
    HELLO hello2 = new HELLO();
    hello2.a1 = 15 
    ..........
    

    hello1.nextHallo = hello2;

    To browse all hello objects in the linked list -

    HELLO helloReference = hello1; //Reference to first object
    while(helloReference!=null)
    {
      helloReference = helloReference.nextHallo ;
      performSomeOperation(helloReference)
    }
    

    Update for OP's question about how to create an instance of nd that gets initialized by the values of HelloPac:

    byte[] HelloPac = new byte[dw];
    nd instance = new nd();
    //HelloPac[0] is first byte
    nd.p=HelloPac[0]; // Ideally should be done inside constructor of nd
    nd.c = HelloPac[1]; //2nd byte
    

    nd.l is a short which is of size 2 bytes so we need to combine 2 bytes HelloPac[2] & HelloPac[3] to obtain short value and assign to nd.l.

    This can be done in following ways - 2 bytes to short java or Convert from 2 or 4 bytes to signed/unsigned short/int

    Note : Normally we would use inbuilt collections like ArrayList and LinkedList to represent same in java. And code snippets provided my here do not adhere to preferred java coding standards.