-
July 22nd, 2011, 02:46 PM
#1
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
-
July 22nd, 2011, 03:26 PM
#2
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.
-
July 22nd, 2011, 03:35 PM
#3
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;
}
Last edited by CppCoder2010; July 23rd, 2011 at 10:18 AM.
Reason: Forgot ending /code tag.
-
July 23rd, 2011, 12:18 AM
#4
Re: looking for a good pointer info
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
Last edited by nuzzle; July 23rd, 2011 at 02:08 AM.
-
July 23rd, 2011, 07:13 AM
#5
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.
-
July 27th, 2011, 05:03 AM
#6
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!
-
July 27th, 2011, 07:07 AM
#7
Re: looking for a good pointer info
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.
Last edited by nuzzle; July 27th, 2011 at 07:29 AM.
-
July 27th, 2011, 08:19 AM
#8
Re: looking for a good pointer info
-
July 27th, 2011, 09:23 AM
#9
Re: looking for a good pointer info
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);
}
-
July 27th, 2011, 09:33 AM
#10
Re: looking for a good pointer info
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
-
July 27th, 2011, 11:02 AM
#11
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.
}
Last edited by CppCoder2010; July 27th, 2011 at 11:06 AM.
-
July 28th, 2011, 01:39 AM
#12
Re: looking for a good pointer info
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.
Last edited by nuzzle; July 28th, 2011 at 01:43 AM.
-
July 28th, 2011, 01:41 AM
#13
Re: looking for a good pointer info
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?
-
July 28th, 2011, 01:53 AM
#14
Re: looking for a good pointer info
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?
-
July 28th, 2011, 02:15 AM
#15
Re: looking for a good pointer info
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|