Why use pointers at all? why not just pass by reference...

why use pointers at all? why not just pass by reference? pointers seem to be a giant meme created specifically to be obscurantist

I had references drilled into me very early on when it came to pointers it all seemed so unnecessary

Other urls found in this thread:

en.wikipedia.org/wiki/Reference_(C++)#Relationship_to_pointers
twitter.com/AnonBabble

kys

>Pajeet gets disciplined by his manager for making long and shit code, when it could be solved easily with pointers

the pointer IS the reference ffs

References are only a feature in C++ and not C, so pointers are still necessary if you're writing C code or using C libraries. On top of that, pointers are nullable while references aren't, which may be desired sometimes. Also, pointers can have new values assigned to them while references can't be updated to refer to something else once they've been initialized. While it's possible to have a reference member variable in a class, it's better to use pointers instead of references for this, as you won't be able to implement assignment operators with references instead of pointers as you can't re-initialize / assign new values to them. Other than that, most of the time references are best for functions that don't copy / gain ownership of your object.

pass by ref isn't the only reason to use pointers
dynamic memory allocation is a thing too

We have reached a new era of wojaks I never expected.

please go study arts or sociology

Most of the times you would be actually passing references. However legacy languages like C doesn't have the idea of reference passing. Plus references are translated to pointers by the compiler.

>le pseudo intellectuals that take time to shitpost but not answer
Into the trash.

check out some data structures like dynamic arrays, linked lists, and binary search trees. If you read up on them and practice implementing them yourself you will see some solid pointer usage.

simple example of something references wont let you do
int strlen(char *s)
{
char *p = s;

while (*p)
++p;
return p - s;
}

First, pointers are a fundamental thing on computers. They are a part of the instruction set architecture of the processor.

The programming language features known as "references" and "pointers" are abstractions.

I'm not sure I really understand your question, though. It seems like you are asking why we expose "pointers" as an abstraction at all (presumably in C++?) when the language provides another abstraction called "references".

This has a clear answer: references do not allow you to do all the things pointers allow you to do. Sometimes those things are necessary. See Wikipedia's list: en.wikipedia.org/wiki/Reference_(C++)#Relationship_to_pointers (which you would have found with a 10 second google of "references and pointers wikipedia").

This is really a very language-specific question. Most languages take very different approaches to abstractions around pointers.

another example

#define VGA_HERP_DERP 0xDF8EF0AF

uint8_t *lol = (uint8_t *)VGA_HERP_DERP;
*lol |= 0x3F;

On a more basic level...pointers point to an object in memory. A reference is a copy of an object in memory (or at least, are copied when passed to a function or data structure). Pointers can be changed. References are immutable.

This is Sup Forums get the FUCK out of here with your big words

To expand on above. Imagine you have a large data structure, like a linked list or binary tree. You need to pass it to a function. If you pass it as a reference, THE WHOLE STRUCTURE gets copied into the function frame. If you pass it as a pointer, only the size of the pointer (meaning only the amount of space needed to hold the memory address) is copied.

copying when passing to a function is pass by value not pass by reference, passing by reference does not copy into a new variable

>A reference is a copy of an object in memory
WRONG

Is this a thing in pluses? Didn't think it would copy that much. In Java every reference is a pointer to an object in memory so my little brain never needs to bother about it.

just ignore him he is very wrong

anyway in c++ by default you pass by value so if you want to pass by reference you need to state that in your function definition also if your reference isn't going to be modified you need to mark it as const as well.

You're confusing reference and value. All C++ compilers implement references as pointers, the differences are thus: You can't perform arithmetic (like traversal) between references. You can't create a null reference. And you can't de-reference a reference without a pointer.

You're all correct, I spouted off confusing byref with byval. Sorry OP.

References are awful and pointers are much better.

myFunc( obj );

Does this take in obj as a cooy or reference? You can't tell without knowing the inplementation.

This:
myFunc( &obj );
Is much bettee

Makes sense. Got really confused for a second. Never liked the explicit use of pointers as opposed to the way java does it when i did my uni assignments, but I understand that they allow you to do more things. But that's why there are different languages: different tools for different needs.

holy shit someone on Sup Forums admitted to being wrong

/board

>byref
>byval
VB kiddie detected

>recognizes VB syntax on sight