92

Why did the C master Dennis Ritchie introduce pointers in C? And why did the other programming languages like VB.NET or Java or C# eliminate them? I have found some points in Google, and I want to listen your comments too. Why are they eliminating pointer concepts in modern languages?

People say C is the basic language and pointers is the concept that makes C powerful and outstanding and makes C still to compete with more modern languages. Then why did they eliminate pointers in more modern languages?

Do you think knowledge of pointers is still important for new programmers? People are using VB.NET or Java these days, which supports more highly advanced features than C (and does not use any pointer concepts) and many people as I see now (my friends) choose these languages ignoring C as they support advanced features. I tell them to start with C. They say it's a waste to learn the concepts of pointers when you're doing the advanced things in VB.NET or Java which are not possible in C.

What do you think?

Updated:

The comments I read on Google are:

  1. The earlier computers were too slow and not optimized.

  2. Using pointers makes it possible to access an address directly and this saves time instead of making a copy of it in function calls.

  3. Security is significantly worse using pointers, and that's why Java and C# did not include them.

These and some more what I found. I still need some valuable answers. That would be greatly appreciated.

15
  • 54
    Java doesn't have pointers? That's not true. Every object reference in Java is, basically, a pointer.
    – quant_dev
    Commented Sep 5, 2011 at 11:45
  • 21
    What quant_dev means is that Java is full of pointers that are used transparently, while programmers can't use them explicitly.
    – sakisk
    Commented Sep 5, 2011 at 12:39
  • 9
    Here's an article from Joel Spolsky that's relevant... joelonsoftware.com/articles/fog0000000319.html Commented Sep 5, 2011 at 12:43
  • 11
    "Why did the C master Dennis Ritchie introduced pointers in c?" Pointers were not introduced in c, they came straight across from assembly practice, name included. Commented Sep 5, 2011 at 16:49
  • 14
    @quaint_dev: Well, Java really does not have pointers. References cannot do everything pointers can do, so attempting to understand pointers in terms of references is not the way to go (and a mistake a lot of programmers learning C or C++ make). Pointers can do arithmetic. References cannot. (A limitation that really stinks every time I'm forced to use Java) Commented Sep 5, 2011 at 16:52

16 Answers 16

131

Back in those days, developers were working much closer to the metal. C was essentially a higher level replacement for assembly, which is almost as close to the hardware as you can get, so it was natural you needed pointers to be efficient in solving coding problems. However, pointers are sharp tools, which can cause great damage if used carelessly. Also, direct use of pointers open up the possibility to many security problems, which weren't an issue back then (in 1970, the internet consisted of about a few dozen machines across a couple of universities, and it was not even called like that...), but became more and more important since. So nowadays higher level languages are consciously designed to avoid raw memory pointers.

Saying that "advanced things done in VB.Net or Java are not possible in C" shows a very limited point of view, to say the least :-)

First of all, all of these languages (even assembly) are Turing complete so in theory whatever is possible in one language, is possible in all. Just think about what happens when a piece of VB.Net or Java code is compiled and executed: eventually, it is translated into (or mapped to) machine code, because that is the only thing which the machine understands. In compiled languages like C and C++, you can actually get the full body of machine code equivalent to the original higher level source code, as one or more executable files/libraries. In VM based languages, it is more tricky (and may not even be possible) to get the entire equivalent machine code representation of your program, but still eventually it is there somewhere, within the deep recesses of the runtime system and the JIT.

Now, of course, it is an entirely different question whether some solution is feasible in a specific language. No sensible developer would start writing a web app in assembly :-) But it is useful to bear in mind that most or all of those higher level languages are built on top of a huge amount of runtime and class library code, a large chunk of which is implemented in a lower level language, typically in C.

So to get to the question,

Do you think knowledge on pointers to the young people [...] is important?

The concept behind pointers is indirection. This is a very important concept and IMHO every good programmer should grasp it on a certain level. Even if someone is working solely with higher level languages, indirection and references are still important. Failing to understand this means being unable to use a whole class of very potent tools, seriously limiting one's problem solving ability in the long run.

So my answer is yes, if you want to become a truly good programmer, you must understand pointers too (as well as recursion - this is the other typical stumbling block for budding developers). You may not need to start with it - I don't think C is optimal as a first language nowadays. But at some point one should get familiar with indirection. Without it, we can never understand how the tools, libraries and frameworks we are using actually work. And a craftsman who doesn't understand how his/her tools work is a very limited one. Fair enough, one may get a grasp of it in higher level programming languages too. One good litmus test is correctly implementing a doubly linked list - if you can do it in your favourite language, you can claim you understand indirection well enough.

But if not for anything else, we should do it to learn respect for the programmers of old who managed to build unbelievable things using the ridiculously simple tools they had (compared to what we have now). We are all standing on the shoulders of giants, and it does good to us to acknowledge this, rather than pretending we are the giants ourselves.

11
  • 5
    This is a good answer but it doesn't really answer the question: "Do the young minds need to learn the pointer concepts?"
    – Falcon
    Commented Sep 5, 2011 at 9:37
  • 11
    +1 Good answer. I'd drop the turing completeness argument though - for practical programming, it's a red herring, as you too note later on. It's computability theory, i.e. turing complete only means there's a program in the (for many languages, infinite) space of potential programs that implements the same algorithm, not whether it's actually feasible or even humanly possible. Just pointing out it's all machine code at the end proves the point just as well without planting the stupid "I can do everything in one language as they're all the same, harhar!" seed.
    – user7043
    Commented Sep 5, 2011 at 10:19
  • 5
    +1 for "And a craftsman who doesn't understand how his/her tools work is a very limited one. " Commented Sep 5, 2011 at 10:55
  • 6
    Also, not understanding the mechanics of pointers (and by extension references) consequently means you don't understands the concepts of shallow/deep datastructure copy, which can cause serious hard-to-track bugs. Even in "modern" high-level languages.
    – Mavrik
    Commented Sep 5, 2011 at 13:11
  • 1
    C was designed to be portable assembler for Unix, i.e. close to the metal.
    – user1249
    Commented Sep 5, 2011 at 17:04
39

I think you need to differ.

Java and other higher level languages did not remove pointers. What they did was to remove plain pointer arithmetic.

In fact, Java still allows a protected and restricted pointer arithmetic: the array access. In plain old C, array access is nothing but dereferencing. It is a different notation, a syntactic sugar, if you will, to clearly communicate, what you're doing.
Still, array[index] is equivalent to *(array+index). Because of that it is also equivalent to index[array] although I suppose some C compilers might give you a warning, if you do that.
As a corollary, pointer[0] is equivalent to *pointer. That's simply because the "pointer to an array" is the address of the first entry of the array and the addresses of the subsequent elements are computed by adding the index.

In Java, plain pointer arithmetics (referencing and dereferencing) don't exist anymore. However pointers exist. They call them references, but it doesn't change what it is. And array access still is exactly the same thing: Look at the address, add the index and use that memory location. However in Java, it will check whether or not that index is within the bounds of the array you originally allocated. If not, it will throw an exception.

Now the advantage of the Java approach is, that you don't have code, that just blindly writes arbitrary bytes into arbitrary memory locations. This improves safety and also security, because if you fail to check buffer overflows and such, the runtime will do it for you.

The disadvantage of this is, that it's simply less powerful. It is possible to do memory safe programming in C. It is impossible to benefit from the speed and the possibilities of unsafe programming in Java.

Actually, there is nothing hard about pointers or pointer arithmetic. They are just normally explained in convoluted ways, whereas all a pointer is, is an index to one giant array (your memory space), all referencing a value does is giving you the index where to find it, all what dereferencing does is to lookup the value at a given index. (This is just a bit simplified, because it doesn't take into account that values are of different size in memory, depending on their type. But that's a circumstantial detail, rather than a part of the actual concept)

IMHO, everybody in our job should be able to understand that, or they are simply in the wrong field.

11
  • 13
    +1 Java and C# still have pointers, and of course NullPointerExceptions
    – jk.
    Commented Sep 5, 2011 at 16:41
  • 5
    Also note that references may point to different areas over time, as the garbage collector moves stuff around. Pointers are usually static.
    – user1249
    Commented Sep 5, 2011 at 20:54
  • 3
    +1: this! And I think there are two hard things to grasp about pointers (in general): indirection (which happens in C, C#, Java, ...) and pointer arithmetic (which doesn't happen in Java in the same way). In my opinion both are important concepts to learn and both are major stumbling blocks for beginners. But they should not be confused: indirection can happen without pointer arithmetic. Commented Sep 6, 2011 at 6:52
  • 2
    Actually, back2dos was right the first time, since (array + index) already takes into account the size of the objects (in C). Commented Sep 6, 2011 at 15:10
  • 4
    @CyberSkull, the answer was giving the syntactic equivalent of array[index], and that's *(array+index). If you want to show how the compiler does things internally, you can explicitly talk about bytes or give the assembly. Commented Sep 7, 2011 at 15:22
24

The concept of pointers is important in the general computer programming body of knowledge. Understanding the concept is good for to-be-programmers or programmers of any language, even if the language does not directly support it.

Pointers have their usage in Data Structures (linked lists) and Database design (Foreign Key).

Languages like VB and C# can pass data by "reference" to methods, which can be thought of as a type of pointer.

Understanding where data is allocated in memory (stack vs. heap) is still important for efficiency of algorithms.

Learning the basics right is important in my opinion.

6
  • I find the general concept to be helpful, but I've never found a situation where I need a pointer (granted I use Java and PHP mostly). The only examples my C++ courses ever came up with for pointers was using them to create more complex data structures like lists and dictionaries that exist in any high level programming language to begin with..
    – Zelda
    Commented Sep 5, 2011 at 13:05
  • 2
    You are correct in a way, but when you pass data to methods, chances are that in some cases you are passing a pointer to the variable. However, the concept of a pointer is useful (in my opinion) regardless of its implementation in a software language.
    – NoChance
    Commented Sep 5, 2011 at 13:15
  • 1
    @SirTapTap: That's because if you learned C++ in some kind of course, they teach you C++. Not the best way to use C++. Pointer arithmetic is usually glossed over because it's something that you can have a passing knowledge of C++ with and not know. But even things like iterating over a general collection are done with pointers in real/idiomatic C++. (As it's the foundation of how the Standard Template Library works) Commented Sep 6, 2011 at 0:28
  • @BillyONeal Pointers were almost half the course, really, I still never found a practical use for (smart) pointers as a programmer, granted I've never needed that direct of control over memory in anything I've done either. Of course there's always the possibility the course was taught poorly, it wasn't exactly my favorite.
    – Zelda
    Commented Sep 6, 2011 at 12:53
  • 1
    @SirTapTap: Practical use: every collection and algorithm in the STL. std::sort, std::partition, std::find, etc. They work with pointers, and they work with objects that act like pointers (iterators). And they work over any general collection; linked lists, dynamic arrays, deques, trees, or any other kind of user defined collection. You can't that kind of an abstraction without pointers. Commented Sep 6, 2011 at 14:42
19

Yes, yes, yes, yes and yes!!!

If you don't know the basics you will NEVER be able to solve the really hard, strange, difficult and complicated problems that come your way.

And if you do understand the basics really well, you are MUCH more marketable in the job market.


I worked once with a chap who had been programming for 10 years, and had no idea how pointers worked. I (much more junior) spent hours at a whiteboard educating him. That opened my eyes. He had NO IDEA about so many basic things.

Know as much as you possibly can.

5
  • But waht are the basics? Assembly, binary code? Commented Sep 5, 2011 at 13:52
  • 5
    Whilst your general point of "know as much as you possibly can" is a sound one, I would question the idea that you will "NEVER be able to solve the really hard, strange, difficult and complicated problems that come your way" if you don't understand pointers. This somehow implies that all difficult problems can be solved using these "magic" pointers, which is not the case. The concepts between pointers are useful to know, but they are not directly essential to many fields of programming.
    – Dan Diplo
    Commented Sep 5, 2011 at 14:10
  • 4
    @Idsa: no, even more basic, many programmer's nowadays don't even know how transistors and logic gates works in the goo' ole' chips, and they surely should have known how electrons moves about and the effect of quantum uncertainty on miniaturization; I haven't even started on quacks, liptons, and bison! and the Hiccups bison particles!
    – Lie Ryan
    Commented Sep 5, 2011 at 15:34
  • 2
    Basics.... things like how stuff is stored. The difference between a byte, a word, how signed and unsigned work. How pointers work. What a character is. How things are coded in ASCII (and these days, Unicode). How a linked list can be created in memory using simple structures only. How strings REALLY work. From these little things, bigger things grow. Commented Sep 6, 2011 at 1:29
  • 5
    Know as much as you possibly can is a good principle, but I think you have the cart before the horse. Good developers strive to learn whatever they can because they're good developers. The yearning for knowledge is a trait of a good developer. It is not the cause of a good developer. Going out and learning as much as you can will not make you a good developer. It will make you a walking encyclopedia, nothing more. If you're a good developer, THEN you can APPLY that knowledge you attained to solve problems. But if you weren't already a good developer, the knowledge won't get you much.
    – corsiKa
    Commented Sep 6, 2011 at 18:01
18

Yes, understanding is important.

A few months back I was programming in C#, and I wanted to make a copy of a list. Of course what I did was NewList = OldList; and then started to modify NewList. When I tried to print out both lists, they were both the same, since NewList was just a pointer to OldList and not a copy, so I was actually changing OldList all along. It didn't take me too long to figure that one out, but some of my classmates weren't that quick and had to be explained why this is happening.

Example:

List<int> a = new List<int>();
a.Add(2);
a.Add(9);
a.Add(8);
a.Add(1);
List<int> b = new List<int>();
b = a; //Does not make a copy, b is just a synonym!
b.Sort();
for (int i = 0; i < a.Count; i++)
{
    Console.WriteLine("a: " + a[i] + " b: " + b[i]);
}

And of course, the result is like this:

a: 1 b: 1
a: 2 b: 2
a: 8 b: 8
a: 9 b: 9

Knowing how to use them isn't that important, yet understanding them is crucial!

3
  • 2
    Instead why to use them and when to use them is most important :)
    – niko
    Commented Sep 5, 2011 at 12:29
  • 5
    "NewList was just a pointer to OldList" - to be precise, both NewList and OldList were just pointers, referring to the same List object. Commented Dec 3, 2011 at 21:19
  • Also important to understand that the new list you created for b no longer has no references to it, and is now garbage.
    – TMN
    Commented Jul 26, 2012 at 15:23
14

Pointer the concept != Pointer the arithmetic != Pointer the syntax

The first matters always, if you need (and you do) understanding of deep/shallow copy, pass by reference/pass by value, etc. The other two matter only if your language du jour allows you to use them.

2
  • 1
    Nowadays you need to know just the basic concept of references, not pointer syntax/math. I learned pointers (with arithmetic&syntax) in C back in the day. The languages I now program in don't deal with C-style pointers that let you do unsafe things. One can understand why in python a=[1,2]; b=a; a.append(3) that both a and b are going to both reference the same object [1,2,3] without knowing stuff like in C the ith element of an array can be referenced by arr[i] or i[arr] as both are *(arr+i). I prefer when the language doesn't let i[arr] be used.
    – dr jimbob
    Commented Sep 6, 2011 at 3:31
  • "The other two matter only if your language du jour allows you to use them." - That sentence is carrying its own self-destructor around. The language du jour is by definition very unlikely to be the same language used tomorrow. And tomorrow you may be faced with a pointer-enabled language. Bottom line? Better one grasps it now, once and forever. It's not that hard, but it is worth it.
    – JensG
    Commented Oct 17, 2014 at 0:55
14

Why did the C master Dennis Ritchie introduce pointers in C?

Because pointers are a very powerful mechanism that can be used in many ways.

And why did the other programming languages like VB.NET or Java or C# eliminate them?

Because pointers are a very dangerous mechanism that can be misused in many ways.

I think programmers should learn about pointers, but from an educational perspective, it is unwise to introduce them early. The reason is that they are used for so many different purposes, it's hard to tell as a beginner why you are using a pointer in a particular circumstance.

Here is an incomplete list what pointers are used for:

  • dynamic allocation (new T)
  • recursive data structures (struct T { T* next; /* ... */ };)
  • iterators over arrays (for (T* p = &a[0]; p != &a[0] + n; ++p) { ... })
  • shared access to objects (T* new_pointer = existing_pointer;)
  • subtype polymorphism (T* pointer_to_base = pointer_to_derived;)
  • legacy call by reference (mutate(&object);)
  • optional types (if (p) { /* ... */ })

Note that using a single mechanism for all of these concepts demonstrates both the power and elegance for the experienced programmer and the great confusion potential for someone new to programming.

1
  • "Why did the C master Dennis Ritchie introduce pointers in C? Because pointers are a very powerful mechanism that can be used in many ways." - I don't know for a fact, but I would guess it has something to do with machine instructions to be wrapped into the C language, and the predecessors of C. Assembler programmers are used to think in pointers, so it would have been a surprise if he hadn't used these well-known powerful mechanisms. Anything else would have been too far away for 1978 or even the 1960s.
    – JensG
    Commented Oct 17, 2014 at 1:06
12

Why? You can write a huge system with forms designer and code generator. Isn't it sufficient? (irony)

And now seriously, pointers are not crucial part of programming in many areas, but they allow people to understand how the internals work. And if we will have no-one who understands how internals work, there will be a situation where SQL2020, Windows 15 and Linux 20.04 will be written in garbage collected virtual machine running over 30 layers of abstraction, with code generated via IDE, in JavaScript.

This is definitely not what I want to see.

So yes, they do have to, definitely!

1
  • 2
    Good humor! +1 and totally agree. Commented Sep 5, 2011 at 13:04
7

Neither Java nor C# eliminated pointers, they have references which are almost the same. What was eliminated is pointer arithmetics, which can be omitted in an introductory course.
No non-trivial application could be done without the concept of pointers or references, so it is worth teaching (No dynamic memory allocation could be done without them).

Consider the following in C++ and Java, and I guess it's not very different in C#:
aClass *x = new aClass();
aClass x = new aClass();
There's not really too much difference between pointers and references, right?
Pointer arithmetics should be avoided unless necessary and when programming with high level models, so there's not much problem there.

6

The professional programmer should master pointers.

The people who want to know programming should learn about its existance and implications, but not necessarily use them.

The people who want to solve personal problems via programming (like me, who use a lot of Python scripts) could very well ignore them at all.

Well, that is my opinion... ;o)

3

Variable address pointers are a specific case of the more generalized concept of indirection. Indirection is used in most (all?) modern languages in many constructs such as delegates and callbacks. Understanding the concept of indirection enables you to know when and how to best use these tools.

3

Absufreakinglutely YES! Everyone who programs needs to understand pointers and indirection.

Pointers are how a large amount of data access gets done in all languages. Pointers are a hardware feature of all microprocessors. High level languages like Java, VB & C# essentially wall off direct access to pointers from the users of the language with references. References refer to objects via the language's memory management scheme (could be a pointer with metadata or just a number for a the memory table, for example).

Understanding how pointers work is fundamental to understanding how computers actually work. Pointers are also more flexible and powerful than references.

For example, the reason why arrays start at index zero is because arrays are actually shorthand for pointer arithmetic. Without learning about how pointers work, many beginning programers don't quite get arrays.

int a, foo[10];
foo[2] = a;

Line 2 in pointer arithmetic would be:

*(foo + sizeof(int) * 2) = a;

Without understanding pointers, one cannot understand memory management, the stack, the heap or even arrays! Additionally, one needs to understand pointers and dereferencing to understand how functions and objects are passed.

TL:DR: Understanding pointers is fundamental to understanding to computers actually work.

2

I think it boils down to the fact that the need to deal with pointers fell away as programmers dealt less with the direct hardware they were running on. For example, allocating a linked list data structure in a way that fit perfectly onto the sequence of 640 byte memory modules that the specialised hardware had.

Dealing with pointers manually can be error-prone (leading to memory leaks and exploitable code) and is time consuming to get right. So Java and C# etc all now manage your memory and your pointers for you via their Virtual Machines (VMs). This is arguably less efficient than using raw C/C++, although the VMs are constantly improving.

C (and C++) are still widely used languages, especially in the High Performance Computing, Gaming and embedded hardware spaces. I'm personally thankful I learned about pointers as the transition to Java's references (a similar concept to pointers) was very easy and I wasn't lost when I saw my first NullPointerException (which should really be called a NullReferenceException, but I digress).

I would advise learning about the concept of pointers as they still underpin a lot of data structures etc. Then go on to chose a language that you love to work in, knowing that if something like a NPE comes up, you know what's really going on.

0

This is the objective truth:

Some languages support direct memory access (pointers), some don't. There are good reasons for each case.

  1. As someone said here, back in the days of C, automatic memory management was not as elaborate as it is today. And people had been used to it, anyhow. The good programmers back then had a much deeper understanding of computer programs than of our generation (I'm 21). They've been using punch-cards and waiting days for some compile-time on the mainframe. They probably knew why every bit in their code existed.

  2. The obvious advantage of languages like C is that they allow you to have finer control over your program. When do you actually need it, these days? Only when you're creating infrastructural applications, such as OS-related programs and runtime environments. If you want to just develop good, fast, robust and reliable software, then automatic memory management is most often your better choice.

  3. The fact is that direct memory access has mostly been abused over the course of software development history. People had been creating programs that leaked memory, and were actually slower because of redundant memory allocation (in C it's easy and common to extend the process' virtual memory space for every single allocation).

  4. These days, virtual machines/runtimes do a much better job than 99% of the programmers at allocating and releasing memory. On top of that, they allow you extra flexibility in the flow you want your program to have, because you're (mostly) not occupied with freeing allocated memory at the right time and place.

  5. As for knowledge. I think it's admirable that programmers know how the environments they program in are implemented. Not necessarily to the smallest details, but the big picture.

I think that knowing how pointers work (at the very least) is interesting. Same as knowing how polymorphism is implemented. Where your process gets its memory from, and how. These are things that have always been of interest to me, personally. I can honestly say that they have made me a better programmer, but I can not say that they're an educational necessity for anyone who wants to become a good programmer. In either case, knowing more is often going to make you better at your work.

  1. The way I see it, if all you're asked to do is create an application in Java or C# or something like that, then you need to focus on proper design and implementation techniques. Testable code, clean code, flexible code. In that order.

Because even if you don't know all the small details, someone who does will be able to change what you'll have created into something that simply performs better. And that's often not a difficult job, once you have a proper, clean and testable design in place (and that's usually most of the work).

If I were an interviewer looking to hire someone for a high-level-language application, those would be the things I'd be most interested in.

Low-level knowledge is a bonus. It's good for debugging and occasionally creating slightly better solutions. It makes you an interesting person, professionally. It grants you some respect in your workplace.

But in today's world, it's no sacred requirement.

-1

For most practical purposes in high level OO languages understanding references is enough, you don't really need to understand how how these languages implement references in terms of pointers.

There are a lot more functional and modern multi-paradigm approaches I'd value much higher than being able to do fancy pointer arithmetic to say, write the 1000th optimized string copy function probably performing worse than String.copy of your std lib any way.

I'd advise to learn a lot more different, higher level concepts first, and branch out to learn languages of different designs to broaden your horizon before attempting to specialize in close-to-hardware stuff.

I often see totally failed attempts to micro-optimize web servlet or similiar code for 5% gain, when caching (memoization), SQL-optimizing, or just webserver config tuning can yield 100% or more with little effort. Fiddling with pointers is premature optimization in most cases.

-1

Definitely, we need to have a thorough concept of pointer, if you really want to be a good programmer. The reason for Pointer concept was a direct access to you value which becomes more efficient and effective with time constraints...

Also, Now a days, considering Mobile Applications, which have very limited Memory we need to use it very carefully so that its operation is very quick with user response... So for this purpose we need a direct reference to the value...

Consider Apple's devices, or Objective C language, which is totally working with pointer concept only. All the variable declared in Objective C is having Pointer. You need to go through Objective C's wiki

1
  • Todays mobile apps have more memory available than some data centers C was conceived on. Many mobile apps are written in Java, or outright html+javascript, all without pointers (the do have REFERENCES). Only a tiny fraction of very specialized programmers ever get to see the underlying OS layers. Commented Sep 28, 2011 at 22:04

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