小编Ind*_*dri的帖子

如何从C函数写入char *

我正在努力写一个char *作为参数传递。我想从函数write_char()向char *写一些字符串。使用以下代码,我遇到了分段错误。

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

void write_char(char* c){
    c = (char*)malloc(11*(sizeof(char)));
    c = "some string";
}

int main(){
    char* test_char;
    write_char(test_char);
    printf("%s", test_char);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c reference function c-strings dynamic-memory-allocation

-5
推荐指数
1
解决办法
190
查看次数