Why does C++ need and use pointers? I know they add power to the language but they make it a lot harder to understand for beginners. Languages like F#, Java, Ruby, Python, Lua, etc. get by just fine without them, and they're quite powerful.
C# has pointers, and I couldn't get by just fine without them. It completely depends on what you are doing, and learning pointers is extremely good for you as a developer. The question isn't why, it is when to use them.
I think iterating through a (file)stream is a lot easier with pointers. And if the data in the (file)stream is always the same size you can just go through it by saying pointer += sizeof(data); But ya... you're right. It's quite hard to understand in the beginning. Maybe there should be a better documentation for it.
This question can start a serious flame war. I asked myself the same thing over and over while trying to get my head around pointers and all the related stuff in c++
On reason languages like ruby, python and others are on another level of coding. They are more abstract farther away from the machine language. But maybe slower.
Another reason is that c++ is a very old language. Many concepts are from times people didn't know any better and I think that is a serious problem. C++ has its advantages it lets you write very fast code and take great shortcuts. But the tradeoff is a language that is hard to understand and programs that look like art but puzzle the persons working with it later.
Maybe C++ isn't for beginners.
Pointers have been around for a long time. They were in Pascal and C. When C++ came out, it would have lost out of the gate without pointers.
All other languages you mention (and you forgot, at least, Java!!!), while each very useful and usable, don't let you get anywhere as close to the machine as C++ (and C) allow: simply put, all of those languages impose on you a higher level of abstraction... which may mostly be fine but will occasionally get in your way.
C++ is a bigger and more complicated language because it allows programming at really low levels of abstraction (very close to the machine) AND at pretty high levels (close to many of the languages you mention) within the same language, indeed within the same source files.
Beginners are well advised to stay FAR away from this amount of power and (inevitably) complication -- but not every programmer is a beginner, and not every bit of code needs (or, actually, can at all stand!-) being within an environment that has "let's protect the poor shmucks from themselves" as a major design goal!-)
Pointers provide a means to pass by reference, which is crucial in some programs that pass large data structures. Instead of passing 100mb, you just pass a small pointer. Languages without "pointers" usually have a mechanism to pass by reference.
Most higher level languages don't have pointers per se, but they do use similar constructs. Here's an example. In C++, if you have something like:
Foo myObject();
you've declared an instance of a Foo, which is always going to be of type Foo. You can't declare a Foo and instantiate another type without using a pointer. However, in C#, you can do it easily:
Foo myObject = new Bar(); // assuming Bar derives from class Foo
The only way to do this in C++ is with a pointer (and thanks to polymorphism). C# is using a pointer of sorts, but abstracting out the messy stuff that makes pointers so "hard" to use.
You're mixing up pointer arithmetic with immutable pointers, AKA, references. All of these languages, including Java, which you didn't mention, do have references. References are slightly less powerful than pointers, but you won't notice this until you get to the stage where you need pointer arithmetic or double or triple pointers. But, under the hood, references are really the same thing as pointers, just that the language won't let you do whatever you want to them (i.e., they're not first-class objects).
Consider this python code:
class X: z=13
y=X()
def f(aa): aa.z = aa.z+1
print(y.z)
f(y)
print(y.z)
f() changes the value of y.z, and this is exactly what would happen in the corresponding C or C++ code written with pointers.
In short, because C++ targets systems programming.
What this means is that C++ is oriented around implementing pretty much everything, from top to bottom.
This includes Operating Systems, where you need to have a level of control of what's happening down to the machine address, not just the VM mapped logical addresses.
This also includes compilers for C++ or any other language, which requires actually implementing the abstractions used by those languages.
This also includes performance constrained settings such as embedded devices programming, or high performance computing, where it's necessary to program in a way that makes very careful use of memory, of instructions, of code size, and any other constrained resource.
Not very many languages target this broad range of tasks. Calling other languages powerful by this standard is rather misleading, because those languages are simply not suited to those tasks.
Why does C++ need and use pointers?
C++ doesn't need anything. Programmers need them and there are programmers who really need them. Like programmers who are building device drivers.
So, there must be some languages that allow raw memory access. C++ happens to be one of those languages.
One reason I've haven't read yet here:
C++ was designed with compatibility to C as a goal, so simply put C++ is a superset to C (which is not 100% true, but almost). As C does have pointers (and, due to its low level nature needs them) C++ also needs to support them.
You can do a lot in C++ without using pointers. Stick with references and higher level things like the STL. Read Accelerated C++ for a good beginner (albeit someone who has programmed in another language) book. C++ can be programmed in a much higher way than C. Stay away from stuff like multiple inheritance, writing your own templates, and pointers at the beginning and you'll be fine. Do learn about virtual functions and object oriented programming from the get go if you are just beginning. Stay away from overloading. Get the first 50% of the language and the hard to learn extra 50% will seem a lot less disconcerting.
Lots of good answers here, but there aren't very many good examples of why it's needed. ... Or why C is sometimes called "Portable Assembly Language."
On the lowest level of abstraction on the machine, there are a large number of hardware functions that are accessed through memory ranges. These memory addresses are often quite standardized with various types of hardware.
For example, when an x86 computer first starts up and BIOS has loaded and started the boot sector, the CPU is in a mode called "16-bit Real Mode" for memory addressing. To access hardware peripherals, there are a few methods, including memory regions and the IN and OUT assembly instructions. Video is accessible at the memory address range 0xA0000-0xBFFFF. Writing to this memory location will draw characters to the screen. Writing to certain regions above 0xC0000 manipulates video BIOS registers, which can, for example, change video modes.
C was initially created to port UNIX to the PDP-11, which uses a memory-mapped I/O scheme similar to the x86 today. UNIX would have never run on the PDP-11 without pointers.
Languages like F#, C#, Ruby, Python, Lua, etc. can't do that without modifying the core language. C and C++ can:
/* Assuming 16-bit DOS environment, 80x25 character video mode. */
char* video = 0xA0000;
video[1] = 'H';
video[3] = 'e';
video[5] = 'l';
video[7] = 'l';
video[9] = 'o'; /* prints "Hello" in the top-left corner of the screen.
Garbage collection (GC) is assumed to be too much overhead for some of the environments where C++ will be used, e.g. embedded systems, operating systems. With high-level references, there is no way for an object to be deleted if it is still accessible. That safety guarantee is made possible by the assumption that GC is there. Without adopting GC as a basic assumption, C++ will always need raw pointers.
Unsurprisingly, one of the most popular C++ questions on this site is "How can I solve the dangling-pointer problem in a general way?", and the most popular answer is to use a reference-counting smart pointer, which is like a poor man's GC. Another answer is to use the right tool for the job - if you aren't doing low-level or embedded systems programming, C++ is probably the wrong tool. C++ is used far more widely in high-level application programming than it probably should be. A lot of people are wasting time debugging memory leaks, memory corruption, etc. just because they're using the wrong tool for the job.
Another reason for using pointers is that they allow the author of a device driver to address a specific region of memory, which is often useful when communicating with devices because certain areas of memory are "mapped" to the hardware device. But again, this is very much a low-level systems programming concern.