我无法弄清楚如何通过函数的参数传回字符串.我是编程新手,所以我想这可能是一个初学者的问题.你能给予的任何帮助都将非常感激.这段代码出错了,我不知道为什么,但是我提供的代码来展示我到目前为止所拥有的内容.
我已将其设为社区维基,因此请随意编辑.
PS这不是功课.
这是原始版本
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
fn(char *baz, char *foo, char *bar)
{
char *pch;
/* this is the part I'm having trouble with */
pch = strtok (baz, ":");
foo = malloc(strlen(pch));
strcpy(foo, pch);
pch = strtok (NULL, ":");
bar = malloc(strlen(pch));
strcpy(bar, pch);
return;
}
int
main(void)
{
char *mybaz, *myfoo, *mybar;
mybaz = "hello:world";
fn(mybaz, myfoo, mybar);
fprintf(stderr, "%s %s", myfoo, mybar);
}
Run Code Online (Sandbox Code Playgroud)
更新这是一个更新版本,其中包含一些建议:
#include <stdio.h>
#include <stdlib.h>
#include …Run Code Online (Sandbox Code Playgroud) 我有字符串: "foo$bar@baz"
我正在寻找一个C程序,它将额外的所有三个子字符串("foo","bar"和"baz")并将每个子字符串放入它自己的字符串中.
PS别担心,这不是功课.