[英]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[])在 Hex2Binary 函数中,myBinary 正确返回,但我需要将整个二进制转换字符串发送到 char* DEtoBinary(char HexDE[]) 的原始调用者
original caller is原来的来电者是
de1Binary = DEtoBinary(DE[0]);
Example my DE[0] = E234567787888888例如我的 DE[0] = E234567787888888
Expected is 111000100011........... But I am getting only binary value of last hex value ie 8 is 1000预期是 111000100011 ....... 但我只得到最后一个十六进制值的二进制值,即 8 是 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.您再次将
char*
复制到char**
这不是一个好主意。 Probably you shall try something like ::可能你会尝试像 ::
strcpy(deBinary[I], Hex2Binary(substring_added1(HexDE, I, 1)));
Check this ::strcpy() return value检查这个 ::strcpy() 返回值
So, I don't think there is any need to save the value returned by strcpy
.所以,我认为没有必要保存
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.其中
deBinary
又是一个char**
:P 但是,据我所知,它只打印前 4 个二进制数字是因为它在第一个二进制表示的末尾遇到了\\0
。
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];
此外,在你的代码中,你声明了这样的东西,
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 ,这是16的阵列
char*
这意味着可以存储16个的地址char
数组,但是INFACT您使用strcpy
复制从返回的字符串Hex2Binary
到指向的位置由deBinary[i]
其还没有被分配任何存储器,因此您将字符复制到从未分配的内存中,这是一个坏主意(但可能适用于您的设备),因此建议您将deBinary
声明为char deBinary[16][5]
或者,而不是使用strcpy
,只需复制Hex2Binary
返回的地址,如
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.因为据我所知,当您向分配给常量内存的
char*
声明类似"1100"
,可能会起作用。 (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!此外,再次谈论
deBinary[]
,您在本地声明了一个char**
并从函数中返回它,这是错误的,因为在函数调用结束后,所有声明的变量都被废弃,因为函数调用退出了堆! So, if you do not need the converted array in the main
, you can try changing the type of the function to void
.因此,如果您不需要
main
的转换数组,您可以尝试将函数的类型更改为void
。 Or else, try dynamically allocating deBinary
or else allocating itself in the main
and passing it as a parameter to the function DEtoBinary
.或者,尝试动态分配
deBinary
或在main
分配自身并将其作为参数传递给函数DEtoBinary
。
You have confused something about char *
and char **
;你混淆了
char *
和char **
; The return type of Hex2Binary(char*)
is char **
, but you return a char *
; Hex2Binary(char*)
的返回类型是char **
,但你返回的是char *
; The return type of DEtoBinary(char[])
is char *
, but you return a char **
; DEtoBinary(char[])
的返回类型是char *
,但您返回的是char **
;
Remember:记住:
char *val
is same as char valp[]
; char *val
与char valp[]
相同;
char *val[]
is same as char **val
; char *val[]
与char **val
相同;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.