I have some data in the memory which is pointed to by a pointer (ptr2data), how do i pass this data to a function which accepts unsigned long/DWORD data types?

unsigned long* ptr2data;
DWORD temp;

ptr2data = (unsigned long*)&SomeArrayOfData[4];
temp = (DWORD) tempSerNum;
AddDevice(7, temp);

The above code passes the address to the data I need to the function, not the data itself!! Please help me fix this.

Thanks

Edited 6 Years Ago by tcstom: n/a

sorry I wrote my code wrong:

void AddDevice(int Foo, unsigned int Bar);
unsigned long* ptr2data;
DWORD temp;

ptr2data = (unsigned long*)&SomeArrayOfData[4];
temp = (DWORD) ptr2data;
AddDevice(7, temp);

thanks Jephtha and deeep, I will give your solutions a try now

Edited 6 Years Ago by tcstom: n/a

I tried changing the code to:

void AddDevice(int Foo, unsigned int Bar);
unsigned long* ptr2data;
DWORD temp;

*ptr2data = (unsigned long) SomeArrayOfData[4];
temp = (DWORD) ptr2data;
AddDevice(7, temp);

as suggested by Jephthah, and it didn't work :(

The method suggested by Deeep wasn't very clear, I tried changing my code to:

void AddDevice(int Foo, unsigned int Bar);
unsigned long* ptr2data;
DWORD temp;

ptr2data = (unsigned long*)&SomeArrayOfData[4];
temp = (DWORD) &ptr2data;
AddDevice(7, temp);

this didn't work either :(

Please help me if you have any more suggestions or see what I have done wrong.
Thanks

I tried changing the code to:

void AddDevice(int Foo, unsigned int Bar);
unsigned long* ptr2data;
DWORD temp;

*ptr2data = (unsigned long) SomeArrayOfData[4];
temp = (DWORD) ptr2data;
AddDevice(7, temp);

as suggested by Jephthah, and it didn't work :(

The method suggested by Deeep wasn't very clear, I tried changing my code to:

void AddDevice(int Foo, unsigned int Bar);
unsigned long* ptr2data;
DWORD temp;

ptr2data = (unsigned long*)&SomeArrayOfData[4];
temp = (DWORD) &ptr2data;
AddDevice(7, temp);

this didn't work either :(

Please help me if you have any more suggestions or see what I have done wrong.
Thanks

The variable temp contains the address of the 4th element of the array. When you pass this variable I think you want to pass it this way

AddDevice(7, *temp);

The variable temp contains the address of the 4th element of the array. When you pass this variable I think you want to pass it this way

AddDevice(7, *temp);

Tried that and it doesn't work, comes up with some sort of data type misalignment error.

The variable temp indeed contains the address of the 4th element of the array, it might be worth noting that I am using pointers because it is a char array and the unsigned long pointer is supposed to return elements 4, 5, 6 and 7 as a single unsigned long.
e.g.
char SomeArrayOfData[8] = {0x54, 0x42, 0x97, 0x53, 0x09, 0x43, 0x11, 0x56};
temp should equal: 0x09431156

it is a char array

You must realize that until you disclosed this vital piece of information all attempts to help were shots in the dark.

Now you may try to go with a

union u {
    unsigned long SomeLongArray[2];
    char SomeCharArray[8];
};

which will force the alignment, and avoid ugly casts.

You must realize that until you disclosed this vital piece of information all attempts to help were shots in the dark.

I now realise that, it's not as slick a solution as I was looking for but it will work.
Thanks

This question has already been answered. Start a new discussion instead.