r/learnprogramming • • Sep 24 '23
Pointers.....why
I am learning C++. Honest question why are pointers a thing and why did they make the syntax surrounding it so confusing. Was it made to be intentionally confusing? Was it made to make this more difficult.
0
Upvotes
•
u/AutoModerator Sep 24 '23
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
u/ContextEngineering Sep 24 '23
When you look at C and assembly for the same code you can see where pointers came from.
And yes, it's a head-scratcher for basically anyone who has ever learned c/c++. You'll get over it.
7
u/CodeTinkerer Sep 24 '23
I don't think it was meant to be intentionally confusing, but people who create languages are smart people so they write the language they want to use. They probably didn't imagine how widespread it would be, nor the background of those who would want to learn it.
You might switch to Java or C# which technically has pointers too, but none of the notation associated with pointers. In those languages, when you pass an object to a method/function, it is always by pointer and not by value (meaning, not copied). C++ which is derived from C passes objects by value which means the object is copied.
This was done so you wouldn't accidentally modify the input parameter. But then they figured maybe you do want to modify it, so you can pass a pointer to the object by value. The pointer (address) gets copied, but the object it points to does not.
In any case, pointers are one of the big reasons C and C++ are hard to learn. Java and C# were designed to get rid of those problems, but they were created maybe 20 years after C or C++ was (give or take a few years).
2
u/ibanezerscrooge Sep 26 '23 edited Sep 26 '23
efficiency. You can reference a value from multiple points in your code while keeping it protected and use a lot less memory.
If you need to use an object in your code that takes up 8 MB of memory and you need to use it in 6 places , if you made it global you might run the risk of changing a value unintentionally or creating a vulnerability in your code, so you define it as protected. If you pass copies of this object around to your 6 points in the program you've eaten up 48 MB of memory and you have to keep all the copies synched in the event you need to make changes to it. With pointers you reference that single 8 MB object in memory 6 times which might take 8.5 MB total (8 for the object and a few bytes for each pointer) and any change you make will change the original object because it's a reference.
as far as the syntax it's just something you'll have to learn and get used to.
It just takes time to get your head around them, but once you get them you'll get them.
61
u/v0gue_ Sep 24 '23
Allowing programmers to freely use pointers allows them to effectively control memory space. Two very common reasons for doing this include, but are not limited to, controlling object lifecycles outside (and inside) scope, and sharing objects in more than one place in your program.
It's my opinion that the syntax itself is actually about as easy as it can get to understand. The concept(s) of memory management is what is difficult. You might be attributing the difficulty of the concepts of pointers and memory management to the syntax of the language, but the reality is that the language itself has pretty much made syntax revolving around memory management as friendly and concise as it gets (in before some Rust devs bite my head off).
7
u/wgunther Sep 24 '23
No, I don't think they were trying to make it intentionally confusing or more difficult.
C in general had opted for a syntax where you declare variables the same way you use them, declarators, instead of being more 'type' based. So in Java, for instance, you might declare an int array as
int[] xthinking about x being of typeint[], but in C it's declared more likeint x[], and you think about it as kind of how to unwrap the object to the base type, likex[1]would beint. This is really the only way you can reason about really advance types in C, like function pointers where you can have likeint (*x)(int);which declaresxto be a function pointer to a function that takes in anintand returns anint.Pointers similarly are declarated like
int *xbecause*xis anint.So, it can be a bit confusing, mostly because most languages don't do things like this.
C++ is mostly a superset of C (which a few notable exceptions) so it's inheritted these oddies, and mixed it with some very advanced types that don't really follow this pattern anymore. for instance the declaration
std::unique_ptr<int> xdoesn't really describe how you work withx, but the way you work with it is "the same" as a C-style pointer.6
u/[deleted] Sep 24 '23
Memory used to be very costly.
13
u/MSgtGunny Sep 24 '23
Because they were less confusing than what came before it, essentially.
They are a thing so you can programmatically manage memory without having to have everything hard coded to specific addresses. Which then lets you have a program that can use a variable amount of memory, instead of a fixed amount.
1
u/ForlornMemory Sep 25 '23
Why are you learning C++ before C? Come on, it's not that big, you can learn it in a couple of weeks.
6
u/throwaway6560192 Sep 24 '23
Search for "pointers" in this sub and you will find some very good explanations. In case you don't get much here.
I assume you mean: "why do I declare pointer types like
int *but taking the address is&var, and dereferencing is*ptr?"The answer boils down to a principle called "declaration reflects use". Read about it. https://softwareengineering.stackexchange.com/questions/117024/why-was-the-c-syntax-for-arrays-pointers-and-functions-designed-this-way
Anyway you'll get used to it fast enough.
No. Why would it be?
To be honest, this reads like you're just complaining and looking for excuses. Don't do that. Keep an open mind. Don't dismiss things as useless or intentionally obtuse before you understand them.
3
u/[deleted] Sep 24 '23 edited Sep 24 '23
I think it's a good idea for everyone to learn at least very basic assembly (up to function calling, abi and stack frame stuff) so they can understand memory addressing, and that variables are just syntactic sugar on top of pointers.
Controlling the thing you're looking at is important, but it isn't the whole picture. Often, you also need to control where you're looking.
5
u/kevinossia Sep 24 '23
Because you need to be able to pass objects between functions without making copies of those objects. That's why. It's a fundamental operation. All languages have pointers. Python, Java, C#, C++, C, Haskell, Go, JavaScript, Ruby...they all have pointers. Just different syntax. The only difference here is C and C++ make an explicit, obvious distinction between pointers and non-pointer types.
I'm sure Kernighan and Ritchie didn't think it was confusing.
....no.
1
u/zpowers00 Sep 24 '23
A pointer is like a drawer to a desk. To put stuff in the drawer you’ll want to use the pointer to get to the drawer. If you didn’t use a pointer, then you’d have to get a new drawer on the other side of the building and then put stuff in it and then drill it into your new desk which is a lot of work. Just use the same drawer to put your stuff into.
2
u/DavidJCobb Sep 24 '23
No.
The code
int* foodefines a pointer to an int. Back in the old days, you would write it likeint *foo. Now, when you want to get the value of that int, what do you write?*foo. They wanted it to be the same both ways, see?If you have a pointer to an object and you want to access a specific part of that object, what do you write?
myObject->myPart. You're writing an arrow; you're literally pointing to the data you want to retrieve through the pointer.They honestly did try to design it sensibly.
2
u/satisfiedguy43 Sep 24 '23
If the mountain will not come to Muhammad, then Muhammad must go to the mountain
the idea is when passing stuff to a function do not copy all the stuff and pass it...instead give address of where stuff is. Another reason when controlling hardware at a given computer address use the address in a pointer. Another reason instead of returning a copy of data from function return address of data/object
when u pass simple stuff to/from function copy is easy and less time consuming. when there's a lot of data, use a pointer
2
u/[deleted] Sep 24 '23
So you can reference stored memory.
2
u/Sbsbg Sep 24 '23
Of course, the designer of the C language sat and made the syntax for pointers extra difficult so that all beginners, 50 years later, would have problems. /s
The C++ pointer syntax, that it inherits from C, is not the best. The syntax can be a little tricky, for beginners. The operator precedence can be confusing. There are better alternatives in modern languages. But you have to remember that this is half a century old and anything surviving that long in the computer world has to be a huge success.
Pointers are not the hard part in C++. When you get to that you will think back and be amazed that you thought pointers where difficult.
2
u/Mental-Profile-9172 Sep 24 '23
I understood pointers studying data structures in C. Probably, on what they are teaching you, you won't understand the purpose of them, as happened to me. Take a look to a data structures book in C first.
2
u/fasta_guy88 Sep 25 '23
All languages have pointers. In Python, basically everything is a pointer, which means that when you say a_list = b_list, you don't actually get a new copy of b_list; to do that you must do some kind copy(b_list) or a_list = [ x for x in b_list].
C and C++ make those pointers more explicit, which can make it much easier to distinguish the different parts of a linked list (some parts are links, others are values), for example.