looking for a good pointer info
hi
im new to c++ and trying to find a good place to learn more about them i already read wiki pg about them but i want more help,tired of seaching at google for a good place so im asking my geeks friend for some help where i can learn more about pointers a good website to learn.
thanks i appreciate, loving this new knowledge
Re: looking for a good pointer info
The easiest way to understand pointers is to think of all the variables you use in your program, not just as words or names, but as little blocks of memory in the program. Think of every variable as an object that has physical size. A char is a small box, an int is a bigger box, a std::string is an elaborate box that you can't see inside of.
A pointer is simply another variable----another box----except that its value type happens to be a location. You can store the location of a different box in there if you wish. You can even store a location in there which doesn't have a normal variable name, which is one of the real powers of pointers.
The aforementioned std::string box interests you. It tells you it's currently holding a 50-letter sentence, but as you look at it, there's no way you could fit 50 of the char-sized boxes into there. You notice a bit of yarn trailing out of the std::string box....and as you follow it, you find 50 char-sized boxes all lined up at the end of the yarn. These are clearly what the std::string was referring to, even though they weren't stored inside it and you didn't even know they existed until you followed the yarn. The yarn, in this scenario, is a pointer.
Re: looking for a good pointer info
A pointer is just what it sounds like, a pointer to a place in memory.
If you declare an integer like below.
You have an integer variable. If you declare a pointer to an integer as below...
You have a pointer that is pointing at nothing, 0 = NULL. This is what's called a NULL pointer.
In order to make the pointer valid, you must make it point at some valid memory. There are a couple of different ways to make this pointer point at valid memory. First of all there is allocating new memory.
Code:
int * pValue1 = new int;
Now you have a pointer pointing at a single integer variable.
To access the memory at this location, you have to dereference the pointer. That is done as follows.
Code:
int * pValue1 = new int;
*pValue1 = 0; // now the integer pointed at by pValue1 is zero.
delete pValue1; // we must cleanup the memory we used by dynamically allocating it.
As I posted in the above code section was the delete operator. You must free the memory allocated by new with delete.
Another way to use a pointer is by taking the address of a variable using the the prefix & operator. This is the address of operator. You can use it as follows, and the pointer will point at the same variable as the integer I declare.
Code:
int nValue1 = 100;
int * pValue1 = &nValue1;
*pValue1 = 200;
if (nValue1 == 200)
{
std::cout << "pValue1 points at the same place in memory as nValue1. It's value is: " << nValue1 << std::endl;
}
// Since pValue1 points at an integer value on the stack, there's no need to delete pValue1.
Pointers can also point at dynamic arrays of memory, and to declare a pointer pointing at an array of memory you can do the following. This introduces the new[] and delete[] operators
Code:
int * pArray1 = new int[100]; // we have pArray1 pointing at 100 integers in memory. The memory is contiguous.
// initialize the array of memory with the corresponding index into the array.
for (int x = 0; x < 100; ++x)
{
pArray1[x] = x;
}
// Now pArray1 is filled with 100 integers in a row in ascending order from 0 to and including 99.
// now delete [] the memory used by pArray1.
delete [] pArray1;
That is for primitive types. I don't know if you are far enough into learning C++ to learn about objects from classes, but you can have pointers to any type in C++, whether it's an integral integer or a complex polymorphic derived class pointed at by a base pointer.
If you want me to post on how to use polymorphic objects pointed at by pointers reply to this thread and I will give you an example of using polymorphism.
EDIT...
Sometimes you need access to a variable in a function from another function. By using a pointer we can access the variable we want to access from another function.
Code:
void Function2(int * pValue)
{
*pValue = 100;
}
void Function1()
{
int nValue = 0;
Function2(&nValue); // using the address of operator to create a temporary pointer pushed onto the stack.
if (nValue == 100)
{
std::cout << "nValue is now 100 set by Function2." << std::endl;
}
}
You can pass an array pointed at by a pointer across function boundaries. You should specify the length of the array as a second parameter.
Code:
void InitializeArray(int * pArray,int nLength)
{
for (int x = 0; x < nLength; ++x)
{
pArray[x] = 100 + x;
}
}
void Function1()
{
// allocate an array of memory.
int * pArray = new array[100];
// initialize the array
InitializeArray(pArray,100);
for (unsigned int x = 0; x < 100; ++x)
{
std::cout << "pArray[" << x << "] = " << pArray[x] << std::endl;
}
// free the memory pointed at by pArray
delete [] pArray;
}
Re: looking for a good pointer info
Quote:
Originally Posted by
ryamjones
i want more help,tired of seaching at google
Why don't you get an introductory C++ textbook. In my view it's easier to learn the basics from a single solid source. This is my personal favourite since I learned C++ from it.
http://www.amazon.com/Absolute-C-3rd...1404166&sr=8-1
This is not the latest edition but it should be good enougth (I used the first edition) and there are cheap used copies to be had. I don't think you should buy a new expensive C++ book at this time because C++ is somewhat in flux right now due to a new standard.
There are other alternatives too like this for example,
http://www.amazon.com/Programming-Pr...1398985&sr=1-9
Re: looking for a good pointer info
A pointer is the same as a shortcut. It isn't the actual data, but it points to it.
Re: looking for a good pointer info
To me, the difficulty is not about the theory, it is about concrete situations, and I guess it is the same for you. My solution is to learn examples, without triying to understand them.
In C and C++ three kinds of symbols are involved: *, &, and [].
Watch carefully pieces of source code where they are used.
The main thing to know is:
Functions can have arguments passed by value or passed by reference (by pointer).
The latter case is where you have to use pointers.
If your variable is already a pointer, then pass it as is.
Else, prefix it with the ampersand &.
In the function, prefix that variable with a star *.
This is the C standard way of doing things.
In C++ you have another possibility with the ampersand inside the function.
Then you have lots of other situations that occur mainly in exercices provided by teachers, and not in real life. This is a way of selecting students. Good luck to you!
Re: looking for a good pointer info
Quote:
Originally Posted by
olivthill2
The main thing to know is:
Functions can have arguments passed by value or passed by reference (by pointer).
That's not true. Pass by reference is not the same as passing a pointer. Pointers can be passed by reference but also by value which they often are.
C supports pass by value only (as does also Java) whereas C++ supports both parameter passing mechanisms.
Re: looking for a good pointer info
Re: looking for a good pointer info
Quote:
Originally Posted by
nuzzle
That's not true. Pass by reference is not the same as passing a pointer. Pointers can be passed by reference but also by value which they often are.
C supports pass by value only (as does also Java) whereas C++ supports both parameter passing mechanisms.
Interesting. I had in mind the following lines of C code where I thought that i was passed by reference:
Code:
int plus_one(int *n)
{
*n += 1;
return(0);
}
int main(int argc, char *argv[])
{
int i;
i = 2;
plus_one(&i);
printf("i=%d", i);
return(0);
}
Re: looking for a good pointer info
Quote:
Originally Posted by
olivthill2
Interesting. I had in mind the following lines of C code where I thought that i was passed by reference:
The "&" used in this context is not the reference operator, it is the address-of operator.
Since addresses are pointers, then you are passing by value (the value being the address-of i).
Regards,
Paul McKenzie
Re: looking for a good pointer info
Paul is right. If you use the address of operator you are passing a pointer which is by value to the function.
You pass by reference doing the following.
Code:
void Function1(int & x)
{
// notice the & after the type of variable before the name of the variable
x += 100;
}
If you were passing a pointer by reference like the below, the address that the pointer points at could be modified.
Code:
void Function2(int *& pX)
{
pX += 100; // dangerous, adds 100 integer widths to the pointer variable, not the integer it points at.
}
Which is essentially doing the same thing as a pointer to a pointer, just the address cannot change.
Code:
void Function3(int ** pX)
{
*pX += 100; // we dereference the pointer to a pointer to get a reference to the pointer, this is just the same as the above code.
}
Re: looking for a good pointer info
Quote:
Originally Posted by
olivthill2
I had in mind the following lines of C code where I thought that i was passed by reference:
No you're passing a pointer by value.
This is why I recommended the OP to invest in a book. It's very important to get the basics right from the beginning.
Re: looking for a good pointer info
Quote:
Originally Posted by
Skizmo
A pointer is the same as a shortcut. It isn't the actual data, but it points to it.
And what if a pointer points to a pointer?
Re: looking for a good pointer info
Quote:
Originally Posted by
Skizmo
A pointer is the same as a shortcut. It isn't the actual data, but it points to it.
Wouldn't that rather be a longcut? Indirect access can hardly be considered shorter that direct access.
And what if a pointer points to a pointer. Does the longcut get even longer or is the pointer considered actual data in that case contradicting your definition?
Re: looking for a good pointer info
Quote:
Originally Posted by nuzzle
Wouldn't that rather be a longcut? Indirect access can hardly be considered shorter that direct access.
I think Skizmo is talking about shortcut icons and symlinks; it is not the shortcut aspect in itself, but the alias aspect of "pointing" to the file/directory/etc that is the analogy.
Re: looking for a good pointer info
Quote:
Originally Posted by
Paul McKenzie
The "&" used in this context is not the reference operator, it is the address-of operator.
Since addresses are pointers, then you are passing by value (the value being the address-of i).
I doubt olivthill2 wasn't aware of that fact, probably he meant "pass by" as a semantical language indipendent concept; in this sense, pointers implement pass-by-reference semantics in C.
besides, if one think at function calls only in terms of initialization rules of its arguments types, then only pass-by-value exist in C or C++, because you always have a source type that is used to initialize the content of a target a type ( yes, references are not object, being not required to have a storage; nonetheless it makes sense identifying their "value" with a non-null pointer ).
indeed, I think that speaking of "passing X by Y" makes no pedagogical sense in C++, because it's not necessary ( as above, you can always represent the function invokation process as a value-initialization mechanism ) and it's also dangerous because it hides the underlying initialization mechanism ( consider copy-elision or special conversions, like array decay, etc... ).
Re: looking for a good pointer info
Quote:
Originally Posted by
superbonzo
indeed, I think that speaking of "passing X by Y" makes no pedagogical sense in C++,
I think it's pedagogically best to stick to conventional terminology.
And it's quite a difference whether a language natively supports pass by reference or you must simulate it.
Re: looking for a good pointer info
Quote:
Originally Posted by
laserlight
I think Skizmo is talking about shortcut icons and symlinks;
Or maybe he's talking about a dog :)
http://www.dogbreedinfo.com/pointer.htm