convert struct into unsigned int *, again convert unsigned int* into struct

There a packet created by struct

struct student {
char name[64];
int age;
int number;
float marks;
};

what would be the syntax to convert struct into unsigned char* ?

--------
in other file
I am getting here same above unsigned int *, I had to reinterpret the unsigned int * into struct and access struct fields/members here.

what would be syntax to convert unsigned int* into struct and access student struct members?

Last edited on

what would be the syntax to convert struct into unsigned char* ?

I'm not sure why you would want to do this. How would you convert a C-string to a pointer to int? You can, but why would you want to do this? Could you provide more context as to why you need to convert it to a pointer to unsigned char?

Anyway, here's how to do it:
1
2
3
4
student s;
strcpy( s.name, "Hello World" );
unsigned char* ptr = reinterpret_cast<unsigned char*>( &s );
std::cout << ptr << '\n';


Hello World



I had to reinterpret the unsigned int * into struct and access struct fields/members here.

You can access members via the member access/dot operator.
http://en.cppreference.com/w/cpp/language/operator_member_access
Last edited on
Topic archived. No new replies allowed.