im currently try to teach myself c++ my question is about pointers i understand how to use but i don't get why you would use them what dose it matter where something is stored in the memory or that you can access the things its pointing to by putting a * in front of it why wouldn't you just manipulate the object directly or am i missing something here? thanks
C++ pointers?
You have come upon a very complex thing. Pointers give the best of us headaches.
They aid in memory improvements, and they can allow a program to change large amounts of information "on-the-fly"
If you are not working on Operating Systems, or working on large programs, then you don't need to worry about it. That's the short of it.
I've never had to use them myself because what I am trying to do is not that big.
Hope that helps.
Reply:C++ began as a superset of C. Whether it is still a superset of C is debated, but it is probably not a separate language: you can access C's libraries and include files in a C++ program and also any of its design features.
The big advantage comes when you don't know how much data you are going to be working with and won't know until run-time. With a corporate database, for example, a company is always looking for new customers, and some customers move on. While some companies can do very well with a database which fixes a maximum size for the number of records they use, it's easier to use new CustomerRecord workrecord and delete workrecord on an as-needed basis. In other words, you code an algorithm for increasing and decreasing the size of the database rather than hard coding the size of the database into your program.
My background is in C. I haven't totally made the conceptual shift to C++ (though I don't have a problem with objects) and I don't want to.
Part of C's design specifications is that everything is passed by value except arrays. In other words when you send an int, float or other variable to a function, it creates a new variable, assigns it the same value as the variable it was sent, and works with that, not with the old variable. When the function ends control passes back to the calling function where the changes and manipulations you made have no effect. In C the only way to pass by reference is to send a pointer to the memory address where the value you want to change is stored. The compiler still creates a new variable, but since it only stores the memory address, its destruction when it goes out of scope will not cause the value at the end of the function to revert to what it was before. C++ does have a pass-by-reference operator, which is the same as the address-of operator in C: %26amp;. While it does make things a little more convenient for a hurried programmer, it also makes writing bad code a little easier, so not everyone uses it. Why make it easy when you should be thinking very hard about what you are doing with it anyhow?
Reply:One of the advantages of pointers are when u want to use an array as an argument of a function. if u give the array itself to the function %26amp; change it in the function, there will be no change in the main array but when you give the pointer of that array as the argument, changes will happen directly on your array %26amp; this is a very important use in programming! If not satisfied u can ask me for more !
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment