what are they really used for, i cant understand them if i dont see any use for POINTERS, ok i thought they keep the memory address of a variable, when i tired to get the valued from a Var in a function it returned "0", i know the Var is local to the func but i assigned the func to the pointer, still went out of scope, i even made the function a pointer function
int num1 = 2; num3 = 3;
int sum = num1 * num2;
int* pPoint = &sum1;
print( pPoint); //short for cout
print( *pPoint );
pPoint = func(); // define funct next....
int func()
{
int num3 = 4, num4 = 5; // i know i defined this after main
int sum2 = num3 * num4; // with prototype before main
pPoint = &sum2; // just put here to be short..
print( pPoint);
print( *pPoint);
return &sum2;
}
print( pPoint);
print ( *pPoint);
// End Main
output: "mem addres 01" 6 // fake address but lets say its accurate for now
funct output: "mem addres 05" 20
output after func: "mem address 05" 0 // <- theres the problem, its suppose to
// be "20" not "0" or at least "6", what it was before going into func.
***************************
***************************
i get compiler errors others ways like "cant convert long** to long* or Long* to Long".
Back to the question, if i cant use pointers to point to the stored value in a local func Var, when it goes out of scope, what are they good for?
One could talk for a long time about pointers. The important thing to note about pointers are:
1. pointers are variables that sit in memory, just like int and other built in type.
2. pointers are a built in type, just like int, char, double
3. a pointer is assigned an address of something or nothing (NULL).
4. pointers can point data and code
Let's look at what you have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
nt num1 = 2; num3 = 3;
int sum = num1 * num2;
int* pPoint = &sum1; // assign the address of sum1 to the pointer
print( pPoint); // print value of pointer, which is the address of sum1
print( *pPoint ); // print value of dereferenced pointer, which is the content of sum1
pPoint = func(); // this should fail as you're trying to put an int into an int*
int func() // maybe this should be int* func()
{
int num3 = 4, num4 = 5;
int sum2 = num3 * num4;
pPoint = &sum2; // assign the address of sum2 to the pointer
print( pPoint); // print value of pointer, which is the address of sum2
print( *pPoint); // print value of dereferenced pointer, which is the content of sum2
return &sum2; // this should fail as you're trying to put an int* into an int
}
print( pPoint); // no point talking about these until you fix the syntax errors
print ( *pPoint);