where i can hold the pointer?

Hello everyone!

So far i know that the pointer is address value to the real variable.
Pointer size depends on operation system right?

for example
32bit systems: 4byte pointers
64bit systems: 8byte pointers
128bit systems: 16byte pointers?

not sure if its true tho, can't find anything about them in internet so im pretty confused and dont understand what is going on.

Anyways, there have to be an variable type in c++ what can hold a pointer.

Let's imagine that the int is the thing im looking for

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct stc1 {
	char *chars;
	int ints;
}

struct stc2 {
	char *chars;
	float floats;
}

int *myholder;
myholder = new int[2];
stc1 a1;
stc2 a2;
myholder[0] = &a1
myholder[1] = &a2


My wish is to hold different types of variable or groups of different type of variables in one variable.

Im developing program in windows now yet it will be used in linux and who knows with what x bit systems.






The closest thing to what you want is a void pointer. By definition, it can point to anything, but it cannot be dereferenced - you must cast it to an appropriate type (generally the type that you initialized it with) to use it.

As an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct A {};
struct B {};

A a;
void* ptr = reinterpret_cast<void*>(&a);

// ...

B b;
ptr = reinterpret_cast<void*>(&b);

//...

A result = *reinterpret_cast<A*>(ptr); // problem: this is legal, but ill-formed 
Last edited on
My wish is to hold different types of variable or groups of different type of variables in one variable.
You might want to look into boost::variant and boost::any.

Also if for some reason you need an integral representation of pointer, there is standard type which can hold it: std::uintptr_t and std::intptr_t
Topic archived. No new replies allowed.