We help IT Professionals succeed at work.

Convert C unsigned char array and uint32_t pointers to real values

cgray1223
cgray1223 asked
on
Hello,

I'm attempting to write a C function to get the id and idx values from the test_id_t_ structure below.  I only have access to the JNI pointer value and need to get the unsigned char array and the unsigned 4 byte Integer value from the below.  The reason I only have the pointer value to the test_id_t_ structure is that I'm attempting to return this structure to Java via JNI (using SWIG).  My C coding experience is very limited.  Thanks!


typedef unsigned char test_id_t[32];
typedef test_id_t id_t;

struct test_id_t_ {
    id_t id;
    uint32_t idx;
};
typedef struct test_id_t_ test_cat_id_t;

Open in new window


Comment
Watch Question

Hi cgray1223,

I'm not sure if I understand correctly, but I guess you mean you have a pointer of another type (i.e. void) pointing to such a test_id_t_ struct - is this correct? If so you can simply cast the pointer (of course you should be sure the pointer is valid and points to a memory block of sizeof(test_id_t_) bytes), i.e.:
void* ptr_data = ...; // pointer to the data
test_id_t_* ptr_struct = (test_id_t_*)ptr_data;
// now you can access the struct's members i.e. via ptr_struct->idx

Open in new window

If otherwise you need i.e. a void pointer to a _test_id_t struct you can simply cast it too, i.e.:
test_id_t_ id;
id.idx = ...;
void* ptr = (void*)&id;

Open in new window

If you need to create a copy of a test_id_t_ and return a pointer you need to use malloc and memcpy and use casting as shown above to access the members.

If none of these is what you need please explain your problem a bit more detailed, best with some code sample where you want to convert those structs and pointers.

Hope that helps,

ZOPPO

Author

Commented:
yes you're assumption was correct it was a void* return type from the function pointing at that structure.