Ok guys so now that I'm quite good at c#, I want to learn c++ and, as far as I can see it's quite easy for me to learn because I know c# already and they both are quite similar, BUT:
Now I understand what pointers are, they are basically the memory address of a variable, right?
But I just don't get what purpose they serve?
Why not just access the variable itself directy? Why do it through a pointer?

So I haven't read that much tutorials and books about c++ but that is a question that really is a worry to me...
So uhm, could you guys tell me the purpose of pointers since I am used to c# and never really used pointers before.
Oh and, I haven't coded a single c++ program, I'm just wondering...

And please don't flame me if that question sounds noobish to all you l33t c++ coders ;)

LOL() will create a duplicate FSCKING_HUGE struct on the stack, which is a complete waste of precious resources.

2. Sometimes you need to modify the original value from a function with a different scope.

void swap(int* a, int* b) {
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}

It is impossible to implement this function without pointers. If you want to modify both "a" and "b", you must have their memory addresses.

3. You can't build advanced data structures without pointers. This is the big one. If you ever get really serious about programming, you'll be implementing trees, heaps, graphs, etc. practically non-stop.

There's also some funky tricks you can do with pointer casting and arrays, but that's mostly C-style antics.

Pointers are used when you create objects on the heap (i.e. with "new") rather than on the stack (i.e. local variables within a function). You put things on the heap when you need them to last longer than the scope they're created in, since local variables are destroyed when they go out of scope.

For example:

class MyClass { ... };
MyClass *createOne() {
MyClass foo; // This instance is on the stack and will be destroyed when createOne() returns
MyClass *bar = new MyClass(); // This instance is on the heap and exists until you delete it
return bar;
}
int main() {
MyClass *obj = createOne(); // This points to the actual object created above, not a copy
// (Do stuff using obj here...)
delete obj; // Destroy the object now that we're done with it
}

The heap is also used when you want to create an array whose size isn't known until runtime. Since the size of an object (struct or class) is fixed at compile time, any variable-sized data held by an object has to be allocated separately. For example, if you create a std::string, it'll contain an internal pointer to a char[] that it allocates with new() and deletes in its destructor.

In C, pointers are also used for passing function parameters by reference, and that can be done in C++ too, but C++ has a better way of doing that (using references).

References are better because they allow for a nicer, seamless syntax and they are a lot safer than pointers. References have a lot of restrictions on them (which is a good thing), so sometimes your only bet is to use a pointer. The general rule is; use pointers whenever you can't do it with references.

There is a lot more to pointers than this, but this is the typical case. For classes and structs you also want to use pointers or references to make them work like you'd expect in C# (see above posts for more on this).

I thought about mentioning that, but wanted to keep my response relatively simple since it's a beginner question. You're right, though, so to fill in the details: variable-length arrays are part of C99, but not C++98; compilers may support them but it's a non-standard extension until C++0x is finalized. They can only be used for arrays that are local variables within a function, not member variables in a struct or class, and they can't be reallocated (e.g. to change their size).

Thanks guys, that helped me to understand pointers way better, and jA_cOp, your example really gave me an idea what they are used for, f.e. to modify variables directly in the memory when that would be impossible without pointers.

Am I not understanding your question properly? I hardly even use C strings.

Since char* can be used for strings, it's basically the same as char[].
So if I wanted to modify the memory allocated to the array, I think I'd have to use a pointer to the array, thus char**. I'm not sure though.

Since char* can be used for strings, it's basically the same as char[].
So if I wanted to modify the memory allocated to the array, I think I'd have to use a pointer to the array, thus char**. I'm not sure though.

A pointer to an array is written char* array[], while a pointer-to-pointer is written as char**.

As arrays can be implicitly cast to pointers, it usually means your code will work the same either way, but you can't dereference an array (without indexing, which can be misleading if it's really just a pointer-to-pointer) without making a pointer out of it first (at which point receiving char** in your function makes more sense).

Since char* can be used for strings, it's basically the same as char[].
So if I wanted to modify the memory allocated to the array, I think I'd have to use a pointer to the array, thus char**. I'm not sure though.

That's actually because you only have a dword to push parameters on a stack to a function, so I'm guessing only primitives are really passed by value.

That's actually because you only have a dword to push parameters on a stack to a function, so I'm guessing only primitives are really passed by value.

Everything but arrays is passed by value in C, including structs.

(And you can allocate more memory on the stack than the processor word size simply by incrementing the stack pointer. If push was the only way to allocate memory on the stack, you'd have some real nasty code for array and struct allocations.)

(And you can allocate more memory on the stack than the processor word size simply by incrementing the stack pointer. If push was the only way to allocate memory on the stack, you'd have some real nasty code for array and struct allocations.)

Oh, thanks.
Explains why some people use pusha and popa instead of doing it manually, I suppose...