Newbie here..why isn't this working?
#include <iostream.h>
int main()
{
int a;
int *ptr;
int *ptr1;
a = 1;
ptr = &a
ptr1 = ptr + 2;
cout << *ptr1 << endl;
return 0;
}
That''s what I''m trying to get to work so I can increase the little knoweledge I have about pointers. So basically I just want *ptr1 to display 3(which it should, shouldn''t it?), but it doesn''t, it just displays some big random number.
If anyone could help me out, and maybe explain what the problem was, it would be greatly appreciated . Thank you!
pointers store the address of something in ram
addition and subtraction on a pointer move the location that the pointer points to
by saying:
ptr = &a
ptr1 = ptr + 2;
you are making ptr1 point at a spot in ram 8 bytes beyond the location that a is stored (as ptr math advances by the datatype the pointer points to, not simply by bytes, and so eight as it advances by 2 4 byte moves)
so the value being printed is the random value in a ram location
addition and subtraction on a pointer move the location that the pointer points to
by saying:
ptr = &a
ptr1 = ptr + 2;
you are making ptr1 point at a spot in ram 8 bytes beyond the location that a is stored (as ptr math advances by the datatype the pointer points to, not simply by bytes, and so eight as it advances by 2 4 byte moves)
so the value being printed is the random value in a ram location
-PoesRaven
I''m pretty new to pointers myself but to do what he hopes to accomplish would he have to dereference the pointer like so?
ptr1 = *ptr + 2;
ptr1 = *ptr + 2;
~punx
no:
ptr1 = *ptr + 2;
would cause ptr1 to point at a memory location 0xA in ram, which would contain more (seemingly) random data
ptr1 = *ptr + 2;
would cause ptr1 to point at a memory location 0xA in ram, which would contain more (seemingly) random data
-PoesRaven
int main(){int a;int *ptr;int *ptr1;a = 1;ptr = &a*ptr1 = *ptr + 2;cout << *ptr1 << endl;return 0;}
*ptr1 should be 3, if that''s what you were trying to get
''There''s something out there....something stupid...''
''I think I''m going to be ill. Is that a problem for you?''
''[You] overestimate my conscience by assuming that I have one.''
- Daria
Check out my raytracer at http://simp-raytracer.sourceforge.net. Download it at http://www.sourceforge.net/projects/simp-raytracer.
March 13, 2003 12:19 AM
If all you want to do is to print 3
change the ptr1 = ptr + 2 to *ptr1 = *ptr + 2 that is saying get the value stored at ptr which in this case is a=1 and add 2 to it then store the value in memory at ptr1.
change the ptr1 = ptr + 2 to *ptr1 = *ptr + 2 that is saying get the value stored at ptr which in this case is a=1 and add 2 to it then store the value in memory at ptr1.
Exactly, Punx. Just add that asterisk in there, and all will be well.
As PoesRaven says, pointers store addresses. So to enter an address into a pointer, you generally use the & operator:
ptr = &a
And to dereference a pointer (to get the value that it points to), you generally use the * operator:
ptr1 = *ptr + 2;
And as PoesRaven said, pointer addition and subtraction adds the number of bytes taken by the declared data type, in this case an integer, which on most compilers these days is 4 bytes.
Hence your code with the corrected line:
ptr1 = *ptr + 2;
will still display a relatively random number.
To do what I believe you''re aiming for, you most likely want something along the lines of:
*ptr1 = *ptr + 2;
This line says that the value pointed to by ptr1 will now equal the value pointed to by ptr, plue 2.
Now the final problem: you''ve not allocated any memory for ptr1. When you declared ptr1, it created (most likely) a 4 byte area in memory that is meant to store an address (as all good pointers should!). But it did not allocate any memory for that pointer (ptr1) to point to. So what you will most likely want to do before the segment of code beginning "a = 1;", is allocate memory for ptr1 to point to. To do this:
ptr1 = new int;
This will allocate memory for an integer and make ptr1 point to it. Now you may continue with your code.
After you''re done with ptr1 and the value it points to, you will need to deallocate said memory (otherwise you have created a memory leak):
delete ptr1;
So now your final code will look something like the following:
BTW, to include source code in your posts, please use the source tags.
This begins source code:
[ source ]
And after your code:
[ / source ]
Just don''t include the spaces in those source tags, and all will be well.
As PoesRaven says, pointers store addresses. So to enter an address into a pointer, you generally use the & operator:
ptr = &a
And to dereference a pointer (to get the value that it points to), you generally use the * operator:
ptr1 = *ptr + 2;
And as PoesRaven said, pointer addition and subtraction adds the number of bytes taken by the declared data type, in this case an integer, which on most compilers these days is 4 bytes.
Hence your code with the corrected line:
ptr1 = *ptr + 2;
will still display a relatively random number.
To do what I believe you''re aiming for, you most likely want something along the lines of:
*ptr1 = *ptr + 2;
This line says that the value pointed to by ptr1 will now equal the value pointed to by ptr, plue 2.
Now the final problem: you''ve not allocated any memory for ptr1. When you declared ptr1, it created (most likely) a 4 byte area in memory that is meant to store an address (as all good pointers should!). But it did not allocate any memory for that pointer (ptr1) to point to. So what you will most likely want to do before the segment of code beginning "a = 1;", is allocate memory for ptr1 to point to. To do this:
ptr1 = new int;
This will allocate memory for an integer and make ptr1 point to it. Now you may continue with your code.
After you''re done with ptr1 and the value it points to, you will need to deallocate said memory (otherwise you have created a memory leak):
delete ptr1;
So now your final code will look something like the following:
#include <iostream.h>int main (){ int a; int *ptr; int *ptr1; ptr1 = new int; a = 1; ptr = &a ptr1 = ptr + 2; cout << *ptr1 << endl; delete ptr1; return 0;}
BTW, to include source code in your posts, please use the source tags.
This begins source code:
[ source ]
And after your code:
[ / source ]
Just don''t include the spaces in those source tags, and all will be well.
I've tried doing that, but all I get is this:
warning C4700: local variable 'ptr1' used without having been initialized
and then the program doesn't run correctly, and doesn't display anything.
Edit - Posted this at the same time as Melraiden, which worked, thanks .
[edited by - Daerus on March 13, 2003 1:32:07 AM]
warning C4700: local variable 'ptr1' used without having been initialized
and then the program doesn't run correctly, and doesn't display anything.
Edit - Posted this at the same time as Melraiden, which worked, thanks .
[edited by - Daerus on March 13, 2003 1:32:07 AM]
Oy.
Long reply there.
Just wanted to point out the main point of my post that seems to have been missed above:
ptr1 doesn''t point to a valid memory address for your program. You do need to allocate memory for it. This is described towards the bottom of my post.
Long reply there.
Just wanted to point out the main point of my post that seems to have been missed above:
ptr1 doesn''t point to a valid memory address for your program. You do need to allocate memory for it. This is described towards the bottom of my post.
And now, a third reply in under a minute.
The reason you''re getting that error is that ptr1 is never made to point to a valid memory location. Where you''ve declared ptr1 (int *ptr1 doesn''t actually tell the compiler to make ptr1 point to a memory address usable by your program. So when you try to use it, there''s a very real possibility that your code will mess with other stuff running on your machine, as it may attempt to access memory being used by another program.
The reason you''re getting that error is that ptr1 is never made to point to a valid memory location. Where you''ve declared ptr1 (int *ptr1 doesn''t actually tell the compiler to make ptr1 point to a memory address usable by your program. So when you try to use it, there''s a very real possibility that your code will mess with other stuff running on your machine, as it may attempt to access memory being used by another program.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement