在 C 中处理多个函数中的指针
我正在尝试从现有代码中创建函数,以使其更清晰,但我遇到了一些问题:
过去是:
int foo(char * s, char * t, char ** out) {
int val = strcmp(s, t);
if (val == 0) {
*out = strdup(s);
return 1;
} else {
*out = strdup(t);
return 5;
}
return 0;
}
现在我有:
int foo(char * s, char * t, char ** out) {
someFunction(s, t, out);
printf("%s", *out);
return 0;
}
int someFunction(char *s, char * t, char **out) {
int val = strcmp(s, t);
if (val == 0) {
*out = strdup(s);
return 1;
} else {
*out = strdup(t);
return 5;
}
return 0;
}
当我尝试执行 printf 时,我遇到了分段错误。 someFunction 应该期待 *out 吗?我想我还是很困惑。
I'm trying to create functions out of existing code in order to make it cleaner, and I'm having some problems:
It used to be:
int foo(char * s, char * t, char ** out) {
int val = strcmp(s, t);
if (val == 0) {
*out = strdup(s);
return 1;
} else {
*out = strdup(t);
return 5;
}
return 0;
}
Now I have:
int foo(char * s, char * t, char ** out) {
someFunction(s, t, out);
printf("%s", *out);
return 0;
}
int someFunction(char *s, char * t, char **out) {
int val = strcmp(s, t);
if (val == 0) {
*out = strdup(s);
return 1;
} else {
*out = strdup(t);
return 5;
}
return 0;
}
And I'm getting segmentation faults when I try to do the printf. Should someFunction be expecting a *out? I guess I'm still confused.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解您的意图,则此代码是“正确的”。 的事情
我假设你正在做一些你真正想做
This code is "correct" if I understand your intent. I assume you are doing something along the lines of
when you really want
我会这样转换它:
I would convert it this way: