Sunday, August 2, 2009

Related to pointer arithmatic in Java.?

Can u answer me what is the substitute code of Java for the following


C code-


first Fuction-


int fun(classobj self)


{


U16 **ppLen;





function(%26amp;p, %26amp;self-%26gt;ArrList);





ppLen = (U16 **)ListIterMoveFirst(%26amp;p);


if (!ppLen)


return 1;


(**ppLen)++; //Problem is here


return 0;


}





2nd Fuc-


U8 fun(calssnm self)


{


U8 rval = *self-%26gt;pCurrent; //pCurrent is a byte[]


self-%26gt;pCurrent++; //Problem is here


return rval;


}





3rd problem-


U16 function(classname self)


{


U16 rval = *((U16 *)self-%26gt;pCurrent);


self-%26gt;pCurrent+=sizeof(rval); //problem is here


return rval;


}





4rth-


int fun(P p1, P p2)//P is a class


{


return (int)p1 - (int)p2;//prob is here.


}





5th-


U16 **ppLen; //ppLen is object reference passing as Arg.


(**ppLen)++; //problem is here

Related to pointer arithmatic in Java.?
First of all i wanted to say you is there are no pointers in java.


So, there is no speech of pointer arithmetic here.





Problem 1: no pointers avl.





Problem 2: No pointers avl.





Problem 3: No sizeOf() operator avl in java because the size of data types is the same in java whatever be the platform you are working on. so, there is no need for a special operator to know its size when the size of fixed. Refer the dicumentation for the size.





Problem 4: You cannot typecast an object to a primitive data type. you can typecast an object of subclass type to superclass type.





Problem 5: no pointers avl.
Reply:There are no pointers in java so there is no perfect match of code. When you create classes or objects in java, the names of those classes are really pointers to the objects but this is handled behind the scenes. For a double pointer, this is like an array of java objects - or an object within an object, depending on your interpretation.





For the first problem, you can create ppLen as a return from another class. Ie.





ppLen = (U16 **)ListIterMoveFirst(%26amp;p);





might be





ppLen = ListIterClass.ListIterMoveFirst(p);





2nd problem is the same except instead of using a function inside the class you just return a member variable.





3rd problem is the same as 2nd problem because sizeof() exists in java as well.





4th problem you dont have to do anything because you can cast to (int) in java as well as C.





5th problem I'm a bit confused as to what it means but if you use the information in this answer and think about it you can figure it out on your own. Good luck!


No comments:

Post a Comment