• Welcome to Autism Forums, a friendly forum to discuss Aspergers Syndrome, Autism, High Functioning Autism and related conditions.

    Your voice is missing! You will need to register to get access to the following site features:
    • Reply to discussions and create your own threads.
    • Our modern chat room. No add-ons or extensions required, just login and start chatting!
    • Private Member only forums for more serious discussions that you may wish to not have guests or search engines access to.
    • Your very own blog. Write about anything you like on your own individual blog.

    We hope to see you as a part of our community soon! Please also check us out @ https://www.twitter.com/aspiescentral

I'm still not getting how pointers work in C++ ?

GoofKing

All your bases are belong to us
I know that a pointer is a variable that holds the memory address of another variable, but even after reading a ton of tutorials on pointers, I'm still not quite getting the whole picture. Is there a down-to-earth example with an every-day program scenario ? Like for an example how books or tutorials on explaining electronics would use a water flow analogy for current and water pressure for voltage (or was pressure amps?)

I can't see the use of pointers in programming :/
 
Um, I'm not completely sure whats being asked. But I'm assuming its a real-life analogy/explanation of how a pointer works that you want.

Basically, the local random access memory (RAM) functions like a city.

Where each block of land is identifiable by their block number. While a family can live on top of a block of land and their location can be found by searching their details in the city database (its often easier this way), sometimes its more useful to find the block itself - regardless of the family that lives there. Consequently, you would look for block numbers instead of family names in the database.

In the analogy, block numbers function as the ADDRESS of the memory itself. In C++, this is really important because you have to manually allocate the level of RAM that a particular variable can have access to for processing - same with de-allocation.

When someone writes (please ignore syntax, its been a while since Ive written c++):
int temp = 5;

It automatically finds an appropriate block to plonk the 'family' temp on top of, and the family holds the value of 5.
Pointers can be used to manually access memory addresses to find the value, ignoring the variable name. In short, its really useful at low level programming when you have to watch the interaction of elements at a RAM level - something that most other programming languages such as java and python ignore.

Pointers are held in very very high regard and can secure most jobs easily as they put you well above anyone else in the field. Very few people can use them properly.
 
How about when you use pointers in everyday programming ? Are they used to mainly for page or virtual memory in an OS ? :/
 
"It may be useful for a program to be able to obtain the address of a variable during runtime in order to access data cells that are at a certain position relative to it." ~ cplusplus.com

In all honesty, pointers don't have much of a use. But they are pretty cool :)
Basically, use pointers when there is nothing else that can get the job done; this won't happen often.

Its good to know how to use them however, its prized knowledge as I said before.
Maybe this will be helpful: c++ - Why use pointers? - Stack Overflow
 
Is there a down-to-earth example with an every-day program scenario ?

When I code in C or C++, it's usually number crunching code, and I tend to use pointers a lot. There are two main things that I use them for. The first is passing arrays into functions. If I have a large list of numbers, this is just the easiest way to get them into a function. Plus, you can use array syntax with pointers, ie this is valid syntax:

void some_function_name(double *); // prototype for the examples

// C-style
double *array;
array = (double *) malloc(2048 * sizeof(double);
array[0] = 3.14; // this is just an example of storing data to the array
some_function_name(array); // this is passing the array into a function

// C++-sytle
double *array = new double[2048];
array[0] = 3.14;
some_function_name(array);

Another thing that I've seen pointers used for is for when you need a function to return more than one thing, and maybe of different data types. So, you can return whatever single value you need to return, and you can put the addresses of the other results you need to store in the function call. The numerical library GSL does this a lot, and it can be useful sometimes.
 
Oh gosh I need to make sense of pointers when I write iOS apps. I did worked with C++ in College like a decade ago. Apple using Objective C for iOS which was old technology they abandoned a long time ago. I guess it good to make use of abandoned technology when a company invested money in it. But it would be nice if Apple choose to use a programming syntax style like Java or C#. I find those languages are easier to learn and better organized . Though I still support C and C++ as it still used for many software, but I don't support Objective C. You can't use Objective C for use outside of Apple.
 

New Threads

Top Bottom