小编hoc*_*han的帖子

C char* array 赋值索引

在创建 strjoin 时,我注意到一个奇怪的现象。在函数 strjoin() 中输出值 strs 时

这些结果被打印出来

123
Run Code Online (Sandbox Code Playgroud)

为什么?

#include <stdio.h>
#include <stdlib.h>

char    *strjoin(int size, char **strs, char *sep)
{
    char *new;

    if (!(new = malloc(sizeof(char) * size)))
        return (NULL);

    printf("%s", strs[1]);
    return (NULL);
}

#include <unistd.h>

int     main(void)
{
    char* b[4] = {"123", "456", "789", "245"};
    char *p;
    int i = 0;
    int size = 5;
    char a[4] = {',', 'a', 'b', '\0'};

    p = *b;

    strjoin(5, &p, a);
}
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
233
查看次数

标签 统计

c ×1