我的目的是從無符號字符數組複製到無符號字符數組。下面的示例代碼可以解釋從無符號字符複製到無符號字符
#include <stdio.h>
#include <string.h>
typedef struct _demo
{
unsigned char something[6];
}demo;
typedef struct _Demo
{
demo d;
}Demo;
typedef struct _copy
{
unsigned char do_something[6];
}copy;
int main()
{
copy *c = new copy;
Demo d1;
for(int i = 0; i < 6; i++)
{
c->do_something[i] = i;
printf("%u", c->do_something[i]);
strncpy(d1.d.something[i], c->do_something[i], sizeof(d1.d.something));
}
return 0;
}
輸出我得到的是:
In function 'int main()':
28:33: error: invalid conversion from 'unsigned char' to 'char*' [-fpermissive]
In file included from 2:0:
/usr/include/string.h:132:14: note: initializing argument 1 of 'char* strncpy(char*, const char*, size_t)'
extern char *strncpy (char *__restrict __dest,
^
28:53: error: invalid conversion from 'unsigned char' to 'const char*' [-fpermissive]
In file included from 2:0:
/usr/include/string.h:132:14: note: initializing argument 2 of 'char* strncpy(char*, const char*, size_t)'
extern char *strncpy (char *__restrict __dest,
我想避免:
d1.d.something[i] = c->do_something[i];
請建議如何進行........ ...
我看到'複製* C =新副本;在你的代碼'。請注意,C和C++是不同的語言。 –
你用'd1.d.something [i]'作爲參數調用strncpy,它是一個'char',而不是'char *'。 'strncpy'接受一個指針作爲參數,而不是char。 –
我注意到David,但我想知道另一種方式 –