Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 返回字符数组的结构打印值两次_C_Arrays_Linux_Struct - Fatal编程技术网

C 返回字符数组的结构打印值两次

C 返回字符数组的结构打印值两次,c,arrays,linux,struct,C,Arrays,Linux,Struct,这是一个简单的程序,其中函数abc返回一个数组。但结果是 Thanks abcdefThanks 为什么会这样?我只想把谢谢打印一次。 我还需要把a的尺寸定为6。在这个程序中,这并不重要,但我在需要的地方进行原始套接字编程。size=6在预定义的头文件中声明。我如何实现它 char *abc() { unsigned char *ch; unsigned char a[7],c[6]; strncpy(a,"Thanks",strlen("Thanks"));

这是一个简单的程序,其中函数abc返回一个数组。但结果是

Thanks
abcdefThanks
为什么会这样?我只想把谢谢打印一次。 我还需要把a的尺寸定为6。在这个程序中,这并不重要,但我在需要的地方进行原始套接字编程。size=6在预定义的头文件中声明。我如何实现它

char *abc()
{
    unsigned char *ch;
    unsigned char a[7],c[6];
    strncpy(a,"Thanks",strlen("Thanks"));
    strncpy(c,"abcdef",strlen("abcdef"));
    ch=malloc(50);
    memset(ch,0,50);
    memcpy(ch,&a,strlen(a));
    memcpy(ch+strlen(a)+1,&c,strlen(c));
    return ch;
}
int main()
{
    char *a;
    a=abc();
    printf("\n%s\n",a);
    printf("\n%s\n",(a+7));
    fflush(stdout);
    return 0;
}

谢谢:)

您忘记了通过附加0字节来终止字符串

unsigned char a[7], c[7]; // 7 = 6+1 since "Thanks" and "abcdef" have 6 bytes
strncpy (a, "Thanks", 6);
a[6] = (char)0;
strncpy (c, "abcdef", 6);
c[6] = (char)0;
您的
c
字符串太短,没有明确的0结尾。因此,您的
strlen(c)
调用
strlen(a)
并没有在您认为应该停止的地方停止,因为没有零终止符,垃圾内存正在破坏您的结果strlen(字符串)不包括零终止符的计数

你应该做以下事情(看看评论)

<>以上是用C++编译的,但应该稍稍调整一下,

这里有一个类似于转储的内存,其中#意味着垃圾

char *abc()
{
    char *ch;
    char a[7],c[7];
    strncpy(a,"Thanks",strlen("Thanks")); // Watch out, strlen(string) doesn't include null terminator
    // a = "Thanks##################################.."
    a[6] = '\0'; // Prevent garbage from uninitialized memory to pester your ch and strlen(a)
    // a = "Thanks\0############################"
    strncpy(c,"abcdef",strlen("abcdef"));
    // c = "abcdef############################"
    c[6] = '\0';
    // c = "abcdef\0############################"
    ch=malloc(50);
    // ch = "###############################"
    memset(ch,0,50);
    // ch = "000000000000000000000000000000"
    memcpy(ch,&a,strlen(a));
    // ch = "Thanks000000000000000000000000"
    memcpy(ch+strlen(a),&c,strlen(c)); // No -1 because you want to cut the terminator off
    // ch = "Thanksabcdef00000000000000000"
    return ch;
}

你真的是这样出去的吗?因为
a
将打印
谢谢�abcdef
a+7
abcdef
。抱歉,已修复和+1:)@很高兴看到您的评论。你总是在这件事上纠正每个人。做得好!!谢谢:)@user3433848这有点困难,但我尽力了。:)@戴维德克宁非常感谢。精彩的解释:)
(char)0
在我看来是一种相当奇怪的书写
'\0'
的方式。
char *abc()
{
    char *ch;
    char a[7],c[7];
    strncpy(a,"Thanks",strlen("Thanks")); // Watch out, strlen(string) doesn't include null terminator
    // a = "Thanks##################################.."
    a[6] = '\0'; // Prevent garbage from uninitialized memory to pester your ch and strlen(a)
    // a = "Thanks\0############################"
    strncpy(c,"abcdef",strlen("abcdef"));
    // c = "abcdef############################"
    c[6] = '\0';
    // c = "abcdef\0############################"
    ch=malloc(50);
    // ch = "###############################"
    memset(ch,0,50);
    // ch = "000000000000000000000000000000"
    memcpy(ch,&a,strlen(a));
    // ch = "Thanks000000000000000000000000"
    memcpy(ch+strlen(a),&c,strlen(c)); // No -1 because you want to cut the terminator off
    // ch = "Thanksabcdef00000000000000000"
    return ch;
}