View Full Version : [C++] Address Types
Serex
07-09-2005, 08:49 AM
Just wondering when working with pointers and you want to store the mem address of an object what type is best to use... can an address be more than one type (i.e int, char ect.) for example if i store a value in memory can it have say an address of 2000 or could it be 20C32.
just trying to clarify some stuff to make it easier for me to understand
You should look for some tutorials on pointers. They aren't easy to learn, but once you get the hang of it they make sense. It's like you have problems and more problems, then one day something in your head clicks and it all seems so easy.
You can printf() addresses of pointers to see what you can learn.
It's always best to store a mem address in a pointer of the same type as the object being pointed to, but a void pointer is good for pointing to more than one type. It can point to any type, even structs and classes. You just have to keep track what it points to for it to be of any use later on :)
int *i;
char *c;
short *s;
long *l;
void *p;
p = i;
p = c;
p = s;
p = l;
shmoove
07-10-2005, 09:36 AM
a void pointer is good for pointing to more than one type. It can point to any type, even structs and classes.
Even functions!
shmoove
Serex
07-10-2005, 05:07 PM
Ok. thanks for your responces so far. i may need a little help from you if possible. shmoove above said that you can point to functions... does this mean u can retrieve information from stored values inside the function?
also i have an array in a function.
int function()
{
string usrQuestions[3];
void *p;
usrQuestions[0] = "How old are you";
usrQuestions[1] = "What is your name";
usrQuestions[2] = "How many pets do you have";
p = &usrQuestions;
return int(p);
}
that will return the address for usrQuestions? i wish to however access the array from main() so i can get the address like
int _tmain(int argc, _TCHAR* argv[])
{
int uQuestAddress;
uQuestAddress = loadQuestions();
cout << "Mem Add: " << uQuestAddress << endl;
}
but getting the value stumps me. i get cast type errors, cannot convert int to *int ect... help would be apreciated
The way I understand it is that you can create a pointer to the address of the function start, but you can't get a pointer to variables within the function because those variables only exist within the function's stack space when the function is called.
If you need to get a pointer address to variables then you should use static global variables or structs in those cases.
Serex
07-11-2005, 04:18 AM
Ok thanks for that aman, muchly apreciated
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.