I have an array of strings as follows:

char *sCmdInt[8];

I need to use elements from the array to pass to another dll. The function takes two LPSTRs as parameters. What I need to do is copy two elements from the array into ints/longs, and then create two LPSTRs from those. I cannot pass the array elements directly, as this causes an access violation:

DIAG_ReadMem32(sCmdInt[1], sCmdInt[2]);

Any suggestions as to how to make totally independent copies? The values held in the array may be hex (can these be converted to int/long?)


There is no reason why this should not work, unless the function DIAG_ReadMem32 is attempting to change the contents of either of those pointers.

char *sCmdInt[8] = {0};
sCmdInt[0] = "123";
sCmdInt[1] = "234";
DIAG_ReadMem32(sCmdInt[0], sCmdInt[1]);

If the values in the array are hex numbers, such as "0x100", then you can use strtol() to convert them to integers

int x;
char str[] = "0x123";
char *ptr = 0;
x = strtol(str,&ptr, 16);

The function does attempt to change the contents of the second parameter. How is this usually resolved?

String literals can not be changed because most compilers put them into read-only memory. How to change that? Notice that you have to make each buffer large enough to hold whatever it is that DIAG_ReadMem32() wants to copy into it. If you don't know exactly how big to make it, take a guess then double it.

char sCmdInt[8][255] = {0};
DIAG_ReadMem32(sCmdInt[0], sCmdInt[1]);

Edited 6 Years Ago by Ancient Dragon: n/a

Is this the only resolution? Isn't is possible to make an independent copy of the value pointed to by sCmdInt[x]? sCmdInt is built up using the strtok function, which only returns pointers.

Of course there are other solutions. And the one you suggested will work too. Just create another buffer and copy the contents of sCmdInt into it.

Edited 6 Years Ago by Ancient Dragon: n/a

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