A variable..is a container of data. It's hard to explain but I really understand all aspects of a variable. Not only from C++ but from other languages as well.
Pointers..so far never been introduced to that, after reading however this seems VERY simple.
OK...a pointer references an area of memory as opposed to a variable..so if you have a variable...you create a pointer for that variable...when you modify the pointer you are modifying the variable as well via it's memory area.
So if you have a function..it generally makes a copy of variables..but if you use pointers then you can modify actual variables themselves using the function..I am familiar with this a little from PHP now that i think of it..but not very often..I know the & symbol in PHP when you start before a variable in a function makes it affect that function..I guess that's a simple wawy of using Pointers..
is my explanation via C++ correct?
So that's the general gist of it, or does it get more advanced than that?
Passing by value, by pointer-to-const, and by const-reference all prevent the caller's actual
parameter from being changed by the function. Passing by pointer (to non-const) and by
(non-const) reference allows the function to change the caller's actual parameter.
Passing by value makes a copy of the caller's actual parameter onto the stack.
References are typically implemented in the compiler as pointers, so passing by reference
and by pointer both store a pointer onto the stack, which is very cheap.
From a performance perspective, pointers and references are equivalent. However, pointers
can be NULL whereas references cannot. A well-implemented function that takes a pointer as
parameter must check the pointer for NULL before dereferencing, and then deal with the
possibility of NULL (often times, but not always, an error case). A well-implemented function
that takes a reference as parameter need not check the reference for NULL since it cannot be,
so the error case is avoided. But accessing the data "referred to" by a pointer or a reference
requires one extra memory access than if passed by value.
From an implementation perspective, accessing the data "referred to" by the pointer requires
the dereference operator (*). Accessin the data "referred to" by a reference is the same as
if the parameter were passed by value. Examples, to illustrate:
1
2
3
int by_value( int x ) { return x + 5; }
int by_pointer( const int* x ) { return x == NULL ? 0 : *x + 5; } // Returns 0 if x == NULL or *x == -5...
int by_reference( const int& x ) { return x + 5; } // Same as by_value case!
In general, pass by value for easy-to-copy types and you don't need to change the caller's
actual parameter. If you don't need to change the caller's actual parameter but it is expensive
to copy onto the stack, use const references. If you do need to change the caller's actual
parameter, use a non-const reference. You should use pointers only when NULL is not an
error case for your function.
OK...a pointer references an area of memory as opposed to a variable..so if you have a variable...you create a pointer for that variable...when you modify the pointer you are modifying the variable as well via it's memory area.
That depends on how you use the pointer, if you use the dereference operator then yes it points to the value that the pointer points to.
If you simply modify the pointer and make it point somewhere else for example, then no you not going to change the value of that variable.
So if you have a function..it generally makes a copy of variables..but if you use pointers then you can modify actual variables themselves using the function..I am familiar with this a little from PHP now that i think of it..but not very often..I know the & symbol in PHP when you start before a variable in a function makes it affect that function..I guess that's a simple wawy of using Pointers..
Se Jsmith's example for a well written explaination.
Thanks that helped bring some things into perspective. Unfortunately though, I can't understand his post. I know a lot of the basics of C++ but a lot of the terms he is using (most of them) are above my current understanding. After I learn quite a bit more and research some of his terminology, I will reread it and see if it makes better sense.
A variable is a reserved area in memory which contains a number (or a complex series of numbers).
Each variable has an address to where it sits in memory.
A pointer is just like any other variable...only instead of holding an integer, or a floating point value, it holds an address. That address "points" (tells you where to find) another variable.
To illustrate the idea:
You can think of memory like a big fat array: int MEMORY[10000];
Now say you create a varialbe int foo. The compiler has to decide where to put this variable in memory. Let's say it decides to put it at address 1234:
1 2 3 4 5
foo = 5;
// that would be the same as this:
MEMORY[1234] = 5;
// because the compiler assigned memory address 1234 to hold the "foo" variable
What pointers do, is they let you access the "MEMORY" array by referring to an address, rather than referring to a variable. So let's say you make a pointer to foo:
int* ptr = &foo;
// since 'foo' sits at address 1234.. &foo == 1234
// (& is the "address of" operator, and as the name suggests, gives you the
// address of the variable in pointer form)
// therefore ptr == 1234
// so this:
*ptr = 6;
// is the same as this:
MEMORY[ ptr ] = 6;
// which is the same as this:
MEMORY[ 1234 ] = 6;
// which is the same as this:
foo = 6;
pointers ARE SOME WHAT EXACT OF EX:1 (GROUPS::A) AS EX:2( A*GROUPS) BUT THE THING IS RESOLUTION SCOPING IS FOR COMMANDING AND ASTERISK IS FOR LOCATION SO ITS LIKE DIVIDING AND MULTIPLYING BUT THE THING IS THEIR APPLICATIONS ARE FOR DIFFERENT PROCEDURES MY PEOPLES.
WOW TANKS FOR THE CLARIFICATION IN CAPSLOCK IT MADE SOOO MUCH MORE SENSE! i WONDER IF EVERYONE TALKS LIKE THIS THEN PERHAPS PEOPLE WILL STOP ASKING DUMB QUESTIONS AND SIMPLY UNDERSTAND! we CAN ONLY HOPE.
Thanks for the help, I appreciate it. It's making a lot more sense.
The dereferencing was the hard part, but I did some reading on it as well and it's starting to make sense.