2

This is a academic question, but I think only professional programmers are the people who can answer this.

Is it true that whatever we can do with pointers, we can do without them as well?

This is something one of my undergrad teachers mentioned and I never really cared to find out, now it seems I have to. I and another fellow programmer had a great argument on this and as I am not really qualified enough with C, I lost the argument (he was against the motion).

We are talking about the language C here.

5
  • 3
    I disagree with the chosen duplicate. This question is about what you can do without pointers in theory, the supposed duplicate is about avoiding pointers being a good idea in practice. Jul 13, 2016 at 15:23
  • 1
    If you want to use dynamic memory, then you have to use pointers. If you want functions to update their parameters, then you have to use pointers (otherwise you have to use globals). A good chunk of the standard library uses pointers as inputs or outputs (scanf, fgets. fopen, strchr, qsort, just off the top of my head). You cannot write effective C code without using pointers; they are a fundamental part of the language. You might as well try to avoid using if statements.
    – John Bode
    Jul 14, 2016 at 15:06
  • "Pointers are the GOTOs of data structures." --apocryphal, but I got it from Grand Admiral Matuszek, Starfleet Command. What the prof is trying to tell you is to THINK about what you are doing BEFORE you start coding it. I've seen some UGLY messes made with pointers, of data structures that were NOT well thought-out. Jul 17, 2016 at 14:30
  • How would you do strings or arrays of unknown length without pointers? Jul 17, 2016 at 19:43
  • Wow! I learnt a lot about C in by reading the answers!
    – Jishan
    Jul 20, 2016 at 18:16

4 Answers 4

4

I don't think you could use the heap (via malloc, or even stack allocation via alloca) without pointers in C as these functions return pointers; so, you'd give up memory allocation. Without memory allocation, you're crippled; might as well write in Fortran '66 -- which by the way is all about writing code using compile-time fixed-size arrays, what fun! (Of course, Fortran doesn't even have structs so you need multiple parallel arrays to do anything useful.)

Now, mind that even arrays in C are treated as pointers as soon as you use them in an expression, such as subscripting/indexing so I'm not sure how you would classify that as to your question; whereas in Fortran, an array is a first-class object that you can use without being considered pointers.

C#, Java (and C++) all have alternatives to pointers (i.e. references) that allow for memory allocation without pointers.

1
  • 8
    And references are just pointers by another name; the difference is that Java and C# don't expose operations on pointer or reference types the way that C and C++ do.
    – John Bode
    Jul 14, 2016 at 15:08
5

Professional programmer's answer: Anything you can do easily, you can do in a hard way. Sometimes in a very hard way that only masochists would consider. Anything you can do with an excavator you can do with a shovel. Anything you can do with a shovel you can do with a teaspoon.

Yes, everything you can do with pointers you can do without pointers. Thanks to Gödel, everything that you can do with an array can be done with a huge integer. Just gazillion times more complicated and slower.

The real question: Why on earth would you do that?

(Obviously you can hide pointers in a programming language by using other things names like references, arrays etc. Under the hood, you still have pointers. )

4

You can use a huge byte-array and replace pointers by array indices. Apart from interacting with external code/hardware that expects pointers, this is equivalent to using pointer.

(Though in C indexing into the array is defined through array to pointer degradation, so it technically still involves a pointer)

2
  • what about pointers to functions?
    – gnat
    Jul 13, 2016 at 15:55
  • @gnat A big switch statement should do the trick. Or just take everything one step further and write a byte-code interpreter. Jul 13, 2016 at 15:58
3

Depends on what you mean by doing.

Strictly speaking, any computer algorithm can be done with a turing machine or for the matter with something that simulates it. And it's not like a turing machine needs pointers, or variables, or even code for the matter.

So yes it can be done, but it would be come with things like slower execution and clarity problems.

Not the answer you're looking for? Browse other questions tagged or ask your own question.