becoming discouraged w/pointers and arrays
I have started my journey on pointers and can some explain in a simple way of idirection. the book says that I cant point to a value of a variable but just the address.
int i; regular variable
int* i_ptr = &i declare the the ptr and initialize it
*i_ptr = 17;
now if I am not mistaken i now has the value of 17.
can someone explain why... the book is not doing a good job.
I know its not as easy as it looks...
My next question is what about using arrays as counters and each element for an individual counter?? could some one explain the concept to me....
quote: Original post by code_fx
I have started my journey on pointers and can some explain in a simple way of idirection. the book says that I cant point to a value of a variable but just the address.
int i; regular variable
int* i_ptr = &i declare the the ptr and initialize it
*i_ptr = 17;
now if I am not mistaken i now has the value of 17.
can someone explain why... the book is not doing a good job.
I know its not as easy as it looks...
My next question is what about using arrays as counters and each element for an individual counter?? could some one explain the concept to me....
I''ll answer the first question as that one I know, but the second question, I''m not even sure I understand what you want to know so I''ll leave it to others. i has the value of 17 because i_ptr is the memory address of i. In other words, lets say i''s memory address is 0x3948, the & when you initialize i_ptr tells i to give it''s address to your pointer. I''m not sure if that clears it up for you or not but I tried...
"And that''s the bottom line cause I said so!"
Cyberdrek
/(bb|[^b]{2})/ that is the Question -- ThinkGeek.com
Hash Bang Slash bin Slash Bash -- #!/bin/bash
[Cyberdrek | ]
int i;
Sets aside memory for a variable of type "int", called i (I'm stating the obvious, but bear with me).
Now, the expression "&i" returns the memory address where i lives. Sometimes you may want to know that memory address so that, say, two different objects can both point at i. The way to declare a pointer to an int, as you know, is:
int *i_ptr;
So, that's a pointer to int type, but it's not been initialised. It needs to point at an address. Since "&i" returns an address, then this:
int *i_ptr = &i
tells i_ptr to point at the address where i is held. If you want to actually access the value of i by using i_ptr, you must dereference the pointer. You do that with the dereferencing operator (the asterisk). Dereferencing simply means that you are asking a pointer what it is actually pointing at. So, when you do:
*i_ptr = 17;
you are assiging 17 to the dereferenced pointer. Since it is pointing at i, you are actually assigning 17 to i.
--
Cats cannot taste sweets.
Edited by - SabreMan on March 1, 2002 3:59:24 AM
March 01, 2002 10:57 AM
Pointers require a pretty abstract way of thinking. Lets try to take the idea of Pointers and apply them to a real life situation that has nothing to do with programming.
Your a traveler.
You want to get to 114 Game Road in a town called Avon.
But you currently have no idea where you are.
How do you get there?
It''s pretty obvious, either use a map, or follow the road signs.
By following the road signs you''ll eventualy end up at your destination.
By using the map you know the exact location of your destination and how to get there so there is no "blind" roadsign following.
In programming terms:
int Avon = 17; // Our destination.
int *Map_Or_RoadSign; // This variable points us to our destination. It is NOT our destination but more like directions on how to get there.
Map_Or_RoadSign = &Avon; // We need directions on how to get to the address of Avon (114 Game Road).
After all of that the Map_Or_RoadSign variable contains "Directions" on how to get to Avon''s address. Now we hit another problem. You''ve got the address but who lives at the address? Well in real life you''d probably already know who lives there, or you''d get a phone book and look it up. When dealing with pointers we do what is called "Derefrencing". This basicaly tells the computer this "I know the address of who I want to see, now show me who lives there". You can "Derefrence" a pointer by adding the * infront of the pointer variable. example:
int i=17;
int *pointer = &i;
cout << *pointer; // This is how you de-refrence a pointer.
"17"
Now just because you derefrence a pointer doesn''t mean it IS 17. The pointer is still pointing at the address, you just specificaly asked for the value of the data rather than the address of it. (if that makes any since to you) Hope this helped you out....as for your second question...well I don''t have a clue as to what your talking about.
Your a traveler.
You want to get to 114 Game Road in a town called Avon.
But you currently have no idea where you are.
How do you get there?
It''s pretty obvious, either use a map, or follow the road signs.
By following the road signs you''ll eventualy end up at your destination.
By using the map you know the exact location of your destination and how to get there so there is no "blind" roadsign following.
In programming terms:
int Avon = 17; // Our destination.
int *Map_Or_RoadSign; // This variable points us to our destination. It is NOT our destination but more like directions on how to get there.
Map_Or_RoadSign = &Avon; // We need directions on how to get to the address of Avon (114 Game Road).
After all of that the Map_Or_RoadSign variable contains "Directions" on how to get to Avon''s address. Now we hit another problem. You''ve got the address but who lives at the address? Well in real life you''d probably already know who lives there, or you''d get a phone book and look it up. When dealing with pointers we do what is called "Derefrencing". This basicaly tells the computer this "I know the address of who I want to see, now show me who lives there". You can "Derefrence" a pointer by adding the * infront of the pointer variable. example:
int i=17;
int *pointer = &i;
cout << *pointer; // This is how you de-refrence a pointer.
"17"
Now just because you derefrence a pointer doesn''t mean it IS 17. The pointer is still pointing at the address, you just specificaly asked for the value of the data rather than the address of it. (if that makes any since to you) Hope this helped you out....as for your second question...well I don''t have a clue as to what your talking about.
March 01, 2002 11:12 AM
Abstract? I''ve never found the abstract... heh. I never got why ppl said they would be hard, maybe I''m wierd.
March 01, 2002 11:16 AM
I know how to use pointers, and you''re examples are confusing me .
Memory Layout (4 bytes of memory):
0x01
......
0x09
int i; (the value of ''I'' is stored at 0x01)
int *i_ptr; (i_ptr can hold an address, this is stored at 0x05)
i_ptr = &i;
(0x05 now stores the value 0x01, so it knows exactly WHERE I is located in memory)
*i_ptr = 17;
* defrefrences i_ptr, so this does this: Checks memory at 0x05, see''s that it has the address 0x01 at it.. so now it goes there in memory... then it sets this memory (0x01) to hold the value of 17. Since ''I'' is stored at 0x01, and i_ptr just modifed the value of the data stored at 0x01, ''I'' has just changed.
Billy - BillyB@mrsnj.com
ps. I don''t think I did much better than anyone else.. . Also, please rephrase your second question, because it doesn''t make much sense.
As I read it, you want to create an array of counters:
Memory Layout (4 bytes of memory):
0x01
......
0x09
int i; (the value of ''I'' is stored at 0x01)
int *i_ptr; (i_ptr can hold an address, this is stored at 0x05)
i_ptr = &i;
(0x05 now stores the value 0x01, so it knows exactly WHERE I is located in memory)
*i_ptr = 17;
* defrefrences i_ptr, so this does this: Checks memory at 0x05, see''s that it has the address 0x01 at it.. so now it goes there in memory... then it sets this memory (0x01) to hold the value of 17. Since ''I'' is stored at 0x01, and i_ptr just modifed the value of the data stored at 0x01, ''I'' has just changed.
Billy - BillyB@mrsnj.com
ps. I don''t think I did much better than anyone else.. . Also, please rephrase your second question, because it doesn''t make much sense.
As I read it, you want to create an array of counters:
int CtrArray[3] = {0,0,0}; //Initialize all values to 0!int ctr;for (ctr=0;ctr!=25;++ctr) //Loop from 0->24{ if (ctr&1) //Is this an odd number? CtrArray[0]++; //If so increment our first int of our array else CtrArray[1]++; //Otherwise, it''s even, so increment our second int if (ctr&4 == 0) //Divisible by 4? CtrArray[2]++; //Increment our third int of our array.second //Each element of the array can be used as a counter...}/*CtrArray[0] holds the number of odd #''s from 0->24CtrArray[1] holds the number of even #''s from 0->24CtrArray[4] holds the number of #''s divisible by 4 from 0->24(divisible by 4 with no remainder!).*/
March 01, 2002 11:19 AM
Err.. sorry, that''s 8 bytes of memory, not 4 .
An int takes up 4 bytes, and a far pointer (defualt in msvc is a far pointer) is 4 bytes.
Billy
An int takes up 4 bytes, and a far pointer (defualt in msvc is a far pointer) is 4 bytes.
Billy
i am in between classes so this maybe a cheesy example.
* asterisk, used a pointer, moreof , the pointer actually holds the address to some variable.
& ampersand, used to refer to address,
==========================================
output.....
10
10
0xAFC1 <--- address
0xAFC1 <--- address
===============================
the array name be itself is pointer to the first position of the array;
uh oh, got to rush to data com....
-----------------------------
"There are ones that say they can and there are those who actually do."
"...u can not learn programming in a class, you have to learn it on your own."
Edited by - cMADsc on March 1, 2002 1:56:30 PM
* asterisk, used a pointer, moreof , the pointer actually holds the address to some variable.
& ampersand, used to refer to address,
==========================================
int main(){using namespace std;int number = 10;int *ptr=0;//creates a pointer and assign it nullptr = # //assigns the address of num to ptrcout << "the number is " << number << "\n";//gets the value stored in number throught the pointer ptr//this is indirectlycout << "the number is " << *ptr << "\n";cout << "the address of number is " << &number << "\n";cout << "the address of the pointer " << ptr << "\n";return 0;}
output.....
10
10
0xAFC1 <--- address
0xAFC1 <--- address
===============================
the array name be itself is pointer to the first position of the array;
int main()int print( int * array){for (int i=0; i<10; ++i){cout << *ptr << "\n";//pritns out value indirectlyp++;//increments the pointer}{int array[10];return 0;}
uh oh, got to rush to data com....
-----------------------------
"There are ones that say they can and there are those who actually do."
"...u can not learn programming in a class, you have to learn it on your own."
Edited by - cMADsc on March 1, 2002 1:56:30 PM
-----------------------------"There are ones that say they can and there are those who actually do.""...u can not learn programming in a class, you have to learn it on your own."
i know arrays can be useful for programming, but particularly in games, why are pointers needed? wot situation requires them to be used?
What about me? What about Raven?
What about me? What about Raven?
quote: Original post by sanguineraven
i know arrays can be useful for programming, but particularly in games, why are pointers needed? wot situation requires them to be used?
Um... arrays are useful for all kinds of programming - a spreadsheet no less than a game. Pointers "point to memory" - so they are highly useful in all kinds of programming as well. What kinds of situations can you think of where getting at memory might be useful?
There are so many situations in C/C++ where pointers are useful and so many others where they are required that it would be quite a lot of work to list them all. Dig into programming with c/c++ and you''ll find out.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement