C新手在这里。 我写道:
typedef unsigned char HBLOCK[SHA512_DIGEST_LENGTH];
typedef unsigned char HPRINTABLE[sizeof(HBLOCK)*2];
typedef struct {
HBLOCK Primary;
HBLOCK *Part;
} FILE_HASH_MAP;
void blockhex(const HBLOCK *, HPRINTABLE *);
打算编写将HBLOCK转换为十六进制字符串的函数。但是当我试图测试个别角色是否可分配时
void blockhex(const HBLOCK *block, HPRINTABLE *printable)
{
printable[0]="6";
};
编译器说
error: incompatible types when assigning to type ‘HPRINTABLE’ from type ‘char *’.
这让我很困惑,因为我希望HPRINTABLE typedef会清楚地表明它是一个数组类型。 而且,当我这样称呼它时
FILE_HASH_MAP fhm;
HPRINTABLE prt;
blockhex(fhm.Primary,&prt);
编译器也说
warning: passing argument 1 of ‘blockhex’ from incompatible pointer type
和
note: expected ‘const unsigned char (*)[64]’ but argument is of type ‘unsigned char *’
当然,那些不是错误,但无论如何我想知道我做错了什么。
答案 0 :(得分:3)
void blockhex(const HBLOCK *block, HPRINTABLE *printable)
{
printable[0]="6";
printable[0]
是char
- 包含sizeof(HBLOCK)*2
元素的数组。 "6"
也是char
- 数组(包含2个元素,即'6'
和'\0'
)。
在C数组中不能分配。
要复制数组,一般使用memcpy()
。
memcpy(printable[0], "6", sizeof "6"); /* Note that sizeof "6" evaluates to 2,
because of the `0`-terminator each
C-"string" carries. */
要复制C-“字符串”,更好/更容易使用strcpy()
,例如:
strcpy(printable[0], "6");
以上相当于:
printable[0][0] = '6';
printable[0][1] = '\0';
有关如何在C中处理“字符串”的更多信息,请访问:http://en.wikipedia.org/wiki/C_string_handling
要让printable
成为char
- 数组,请将其声明为HPRINTABLE printable
,并且在调用{&
时不要使用prt
上的地址运算符blockhex();
{1}}。
block
相同。