简体   繁体   中英

returning array of character pointer in C

I am using below function to convert Decimal to binary

char** DEtoBinary(char HexDE[])
{
    printf("HexDE = %s\n", HexDE);
    int I;
    char* deBinary[16];
    for (I = 0; I <= 15; I++)
    {
        //deBinary = deBinary + Hex2Binary(HexDE.Substring(I, 1));
        deBinary[I] = strcpy(deBinary, Hex2Binary(substring_added1(HexDE, I, 1)));

    }
    printf("deBinary = %s\n", deBinary);
    return deBinary;

}

Hex to binary function

char *Hex2Binary(char* DE)
{
    printf("Inside DE = %s\n", DE);

    char *myBinary;
    long val = strtol(DE, NULL, 16);

    switch(val)
    {
        case 0:
         myBinary = "0000";
         break;

        case 1:
         myBinary = "0001";
         break;

        case 2:
         myBinary = "0010";
         break;

        case 3:
         myBinary = "0011";
         break;

        case 4:
         myBinary = "0100";
         break;

        case 5:
         myBinary = "0101";
         break;

        case 6:
         myBinary = "0110";
         break;

        case 7:
         myBinary = "0111";
         break;

        case 8:
         myBinary = "1000";
         break;

        case 9:
         myBinary = "1001";
         break;

        case 10: //A
         myBinary = "1010";
         break;

        case 11: //B
         myBinary = "1011";
         break;

        case 12://C
         myBinary = "1100";
         break;

        case 13://D
         myBinary = "1101";
         break;

        case 14://E
         myBinary = "1110";
         break;

        case 15: //F
         myBinary = "1111";
         break;

    }
    printf("myBinary = %s\n" ,myBinary);
    return myBinary;
 }

In Hex2Binary function, myBinary is returning properly, but I need to send whole binary converted string to original caller of char* DEtoBinary(char HexDE[])

original caller is

de1Binary = DEtoBinary(DE[0]);

Example my DE[0] = E234567787888888

Expected is 111000100011........... But I am getting only binary value of last hex value ie 8 is 1000

Few things which I still see wrong are ::

deBinary[I] = strcpy(deBinary, Hex2Binary(substring_added1(HexDE, I, 1)));

You are again copying a char* to a char** which is not a good idea. Probably you shall try something like ::

strcpy(deBinary[I], Hex2Binary(substring_added1(HexDE, I, 1)));

Check this ::strcpy() return value

So, I don't think there is any need to save the value returned by strcpy .

Moreover, in your print statement you print

printf("deBinary = %s\n", deBinary);

where deBinary is again a char** :P But, from what I understand, that it only prints first 4 binary digits is because it encounters \\0 at the end of the first binary representation.

So, probably you shall try to do::

for(int i = 0; i < 16; i++) {
    printf("%s ", deBinary[i]);
}

This, might actually solve your problem.

Moreover, in your code, you declare something like this, char* deBinary[16]; , which is an array of 16 char* which are meant to store the address of 16 char arrays, but infact you use strcpy to copy the string returned from Hex2Binary to the location pointed by deBinary[i] , which has not been allocated any memory, so you are copying characters to memory you never allocated, which is a bad idea (but might work on your device), so it is advisable either you declare your deBinary as char deBinary[16][5] or, instead of using strcpy , simply copy the address returned by Hex2Binary like

deBinary[i] = Hex2Binary( ... );

Because as far as I know, when you declare something like "1100" to a char* that is allocated to the constant memory, and might work. (I am not sure about this, if someone can comment about this and help it would be great).

I wish this could help!

EDIT ::

Moreover, talking again about the deBinary[] , you have locally declared a char** and returning it from the function, which is wrong since after a function call gets over, all the variables declared are scrapped since the function call gets out of the stack! So, if you do not need the converted array in the main , you can try changing the type of the function to void . Or else, try dynamically allocating deBinary or else allocating itself in the main and passing it as a parameter to the function DEtoBinary .

You have confused something about char * and char ** ; The return type of Hex2Binary(char*) is char ** , but you return a char * ; The return type of DEtoBinary(char[]) is char * , but you return a char ** ;

Remember:

char *val is same as char valp[] ;

char *val[] is same as char **val ;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM