Help with C++ Pointers
Recently, I''ve been bumbling around with all sorts of languages. I''ve just kinda "picked up" some Qbasic, but that''s old. I know a good deal of java-script, but that''s mainly for websites and web app''s. The only major language I''ve actually been studying is C++. Unfortunetly, ever since I began learning C++, I''ve been having a lot of trouble trying to understand pointers and their uses!
Does anyone know any good tutorials or doc''s on C++ pointers. Even just your own comments would be great!
Thanks!
P.S. I think I''m going to stick to C++ now :-)
(okay... maybe just a little more VB...)
pointers seem to cause more difficulties than they should for newbies.
Believe it or not, pointers are very simple. A pointer is essencially a integer data type whos value is the location in memory of an object. To clarify, memory (ram) is like a huge array of bytes. And a pointer is the number of the first element in that array which contains the data of the object you point to.
Read that several times over until you are pretty sure you understand it.
Get it? ok, now look at a picture ;-)
Now the benifits of pointers should be clear. If you have an object that is half a meg in size, it is a whole lot faster to pass around a pointer or reference (a special type of pointer) which is only 32 bits as opposed to a half meg object.
If you don''t get it spend some more time learning about how computers work internally.
Check out www.howstuffworks.com for some information on "How C Works" and a few other interseting things.
-SniperBoB-
Believe it or not, pointers are very simple. A pointer is essencially a integer data type whos value is the location in memory of an object. To clarify, memory (ram) is like a huge array of bytes. And a pointer is the number of the first element in that array which contains the data of the object you point to.
Read that several times over until you are pretty sure you understand it.
Get it? ok, now look at a picture ;-)
MEMORY012345678901234567890123456789 ^ Pointer
Now the benifits of pointers should be clear. If you have an object that is half a meg in size, it is a whole lot faster to pass around a pointer or reference (a special type of pointer) which is only 32 bits as opposed to a half meg object.
If you don''t get it spend some more time learning about how computers work internally.
Check out www.howstuffworks.com for some information on "How C Works" and a few other interseting things.
-SniperBoB-
Brandon Bloomhttp://brandonbloom.name
ted jensens pointer tutorial for C is the best i have ever read.
http://gpp.netfirms.com/cgi-bin/resourceCanada.cgi?c_tutorialPointersArrays
Its my duty, to please that booty ! - John Shaft
http://gpp.netfirms.com/cgi-bin/resourceCanada.cgi?c_tutorialPointersArrays
Its my duty, to please that booty ! - John Shaft
August 06, 2002 04:28 AM
this is probably easiest to explain if you know structs/classes first.
Are you a person? Then make yourself a struct:
struct Person
{
};
do you have arms, and are they part of you? So add those:
struct Person
{
Arm left;
Arm right;
};
Do you have a friend? Is he part of you? No he isn''t, use a pointer:
struct Person
{
Arm left,right;
Person* friend;
};
I want to give my friend a kitten:
void GiveKittenToFriend(Kitten k) {/*function body goes here*/}
however there is a mistake. If you pass a parameter to a function it makes a copy of it (if you don''t know that then you aren''t ready for pointers). I don''t want to make a copy of the Kitten, I just want to let him know that it is his. So I pass a pointer instead
void GiveKittenToFriend(Kitten* k);
and I call it like so: GiveKittenToFriend(&my_kitten);
but maybe I don''t want to give it to my friend, maybe I want to be able to give it to whomever. Instead of hard-coding in my friend I''ll make the person I give it to be a pointer too.
GiveKittenToPerson(Kitten* k, Person* p);
well does that help you any?
Are you a person? Then make yourself a struct:
struct Person
{
};
do you have arms, and are they part of you? So add those:
struct Person
{
Arm left;
Arm right;
};
Do you have a friend? Is he part of you? No he isn''t, use a pointer:
struct Person
{
Arm left,right;
Person* friend;
};
I want to give my friend a kitten:
void GiveKittenToFriend(Kitten k) {/*function body goes here*/}
however there is a mistake. If you pass a parameter to a function it makes a copy of it (if you don''t know that then you aren''t ready for pointers). I don''t want to make a copy of the Kitten, I just want to let him know that it is his. So I pass a pointer instead
void GiveKittenToFriend(Kitten* k);
and I call it like so: GiveKittenToFriend(&my_kitten);
but maybe I don''t want to give it to my friend, maybe I want to be able to give it to whomever. Instead of hard-coding in my friend I''ll make the person I give it to be a pointer too.
GiveKittenToPerson(Kitten* k, Person* p);
well does that help you any?
Pointer: pointing to somewhere in the memory.
Memory: array of bytes, used and unused.
Used: Allocated bytes by a program and can be used and accessed.
Unused: Rubbish. Can''t be accessed.
Most beginners don''t know that there are Used and Unused memory.
int* ptr;
ptr is a pointer
a pointer that points to somewhere in the memory.
a pointer that points to an array of UNUSED bytes.
Remember, ptr is just a pointer...a pointer...a pointer
if you do this:
*ptr = 10;
which means, set the content pointed by ptr to 10. The above code will fail, because ptr is still pointing to an unused memory address.
char* string;
strcpy(string, "Hello World");
Same thing happens here. string points to an unused memory address.
So, how do we make it points to a Used memory address. We tell the system to allocate a memory for our program and let our pointer points to it.
int* ptr;
ptr = new int;
*ptr = 10;
now, ptr points to a Used memory of type int, thus assigning 10 to it is not a problem anymore.
char* string;
string = new char [80];
strcpy(string, "Hello World");
now, string points to an array of Used memory of type char (80 elements total), thus assigning "Hello World" onto it is not a problem since "Hello World" is 12 characters long (11 + 1 null).
Anyway, Used and Unused are just terms that I made up. Perhaps, Valid and Invalid is a more appropriate use.
Hope this helps
My compiler generates one error message: "does not compile."
Memory: array of bytes, used and unused.
Used: Allocated bytes by a program and can be used and accessed.
Unused: Rubbish. Can''t be accessed.
Most beginners don''t know that there are Used and Unused memory.
int* ptr;
ptr is a pointer
a pointer that points to somewhere in the memory.
a pointer that points to an array of UNUSED bytes.
Remember, ptr is just a pointer...a pointer...a pointer
if you do this:
*ptr = 10;
which means, set the content pointed by ptr to 10. The above code will fail, because ptr is still pointing to an unused memory address.
char* string;
strcpy(string, "Hello World");
Same thing happens here. string points to an unused memory address.
So, how do we make it points to a Used memory address. We tell the system to allocate a memory for our program and let our pointer points to it.
int* ptr;
ptr = new int;
*ptr = 10;
now, ptr points to a Used memory of type int, thus assigning 10 to it is not a problem anymore.
char* string;
string = new char [80];
strcpy(string, "Hello World");
now, string points to an array of Used memory of type char (80 elements total), thus assigning "Hello World" onto it is not a problem since "Hello World" is 12 characters long (11 + 1 null).
Anyway, Used and Unused are just terms that I made up. Perhaps, Valid and Invalid is a more appropriate use.
Hope this helps
My compiler generates one error message: "does not compile."
My compiler generates one error message: "does not compile."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement