Pointer Magic : Help Pls
lets say i have:
-------------------------------------------------------------------
int pointer;
int array[20];
pointer = array;
------------------------------------------------------------------
How do i find out the size of the array variable by just using the pointer variable. I thought it was sizeof(*pointer), but that seems to work only for a non-array.
If you have:
You can''t find the size of th array (but you already know it anyways).
Now, if you had:
You still can''t find the size of the array using the pointer alone. In fact, you can never find the size of the array unless you have a special "marker" element to signify the end, or you store the array size somewhere.
For all of your simple-to-intermediate array needs in C++, I suggest you use std::vector (part of the Standard Template Library, STL). See the STL link in my .sig for more info. For all of your simple-to-intermediate string needs, use std::string. Same link.
[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
int pointer;int array[20];pointer = array;
You can''t find the size of th array (but you already know it anyways).
Now, if you had:
int *ptr; // note the *!int *array;// array is initialized to some size using either new or mallocp = array;
You still can''t find the size of the array using the pointer alone. In fact, you can never find the size of the array unless you have a special "marker" element to signify the end, or you store the array size somewhere.
For all of your simple-to-intermediate array needs in C++, I suggest you use std::vector (part of the Standard Template Library, STL). See the STL link in my .sig for more info. For all of your simple-to-intermediate string needs, use std::string. Same link.
[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement