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?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.