Reputation: 238
Ex.
int pages[3];
int *page_ptr;
page_ptr = pages;
//
test a;
*page_pointer = a;
I want to take a structure (test
) of same size as pages
and place it at the memory location of pages
. Is this possible?
Upvotes: 0
Views: 72
Reputation: 206697
I want to take a structure (test) of same size as pages and place it at the memory location of pages. Is this possible?
Yes, it is possible. However, instead of using
*page_pointer = a;
you should use memcpy
.
memcpy(page_pointer, &a, sizeof(pages));
Update, in response to comment by OP
I think you are asking whether you can do something like:
#include <iostream>
struct foo
{
int a;
int b;
int c;
};
int main()
{
int array[3] = {10, 20, 30};
foo* ptr = static_cast<foo*>((void*)array);
std::cout << ptr->a << ", " << ptr->b << ", " << ptr->c << std::endl;
}
I tried it on g++ 4.9.2 and works OK. I am not sure whether it violates any type aliasing rules.
Upvotes: 1