-4

is it possible to convert any program written in C using pointer into another c program that does not contain any pointers?If yes, can we automate the process?

i read a few papers on c to java bytecode compilation and found that a major issue was "the pointer problem".so i was thinking that if the above process could be done,then it could be included like a preprocessing step(though it itself may be big task) and then it may be simpler to try converting to jvm bytecode...

thanks in advance

user1994
  • 53
  • 5
  • You can't automate it. You might be able to rewrite it by hand. Some more details in the question would help, if you want more of an answer than that. – yellowantphil Jan 22 '15 at 18:16
  • automation is not possible for this scenario. – Bector Jan 22 '15 at 18:23
  • Is this a computer science theoretical question or do you want to solve a real problem. While you can transform a program (not sure if there is an existing tool to do it) it might not be possible to call existing APIs if they require pointers. (and youi might not be able to simulate all APIs). A transformed program might not run as fast. – eckes Jan 22 '15 at 18:24
  • You may find this thread helpful: http://stackoverflow.com/questions/3473754/convert-c-to-java – Ruud Helderman Jan 22 '15 at 20:08

2 Answers2

0

Pointers are rather central to C. While C may be turing-complete without pointers, it's not practical to rewrite arbitrary C without them.
Things you can't do without pointers:
-dynamic (manual) memory allocation.
-passing by reference.

Given that arrays decay into pointers a the drop of a hat, you also couldn't use arrays practically, so you are left with automatic, static and global variables that cannot be arrays.
tl;dr: No

EOF
  • 6,273
  • 2
  • 26
  • 50
0

In theory you can, by simulating individual data structures or even the entire memory (static data, heap and stack) with arrays. But the question is whether this is very practical; it may involve having to rewrite every pointer-based standard library function you need.

Anyway, there's a nice explanation on Wikipedia:

It is possible to simulate pointer behavior using an index to an (normally one-dimensional) array.

Primarily for languages which do not support pointers explicitly but do support arrays, the array can be thought of and processed as if it were the entire memory range (within the scope of the particular array) and any index to it can be thought of as equivalent to a general purpose register in assembly language (that points to the individual bytes but whose actual value is relative to the start of the array, not its absolute address in memory). Assuming the array is, say, a contiguous 16 megabyte character data structure, individual bytes (or a string of contiguous bytes within the array) can be directly addressed and manipulated using the name of the array with a 31 bit unsigned integer as the simulated pointer (this is quite similar to the C arrays example shown above). Pointer arithmetic can be simulated by adding or subtracting from the index, with minimal additional overhead compared to genuine pointer arithmetic.

Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45
  • @EOF This approach requires only array indexing (`simulatedMemory[simulatedAddress]`). This translates nicely to Java (OP's scenario), regardless of its true semantics in C. – Ruud Helderman Jan 22 '15 at 19:56