c-分割故障错误是由于调用用户在终端中输入的文本的函数而引起的
摘要
我正在尝试传递用户在终端中放置的文本,并将其传递到名为printString()的函数。我不完全了解C,但我认为我与不在堆中的指针有关。任何帮助都将得到认可!
#include <stdio.h>
void printString();
int main (int argc, char* argv[]) {
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, &argv);
}
return 0;
}
void printString(int commandAmount, char* argv[]) {
printf("the word is %s," , argv[commandAmount]);
}
./shortExample example
Segmentation fault (core dumped)
Summary
I am trying to pass the text that user has put in the terminal and pass it to the function named printString(). I don't fully understand C but I think I has to do with the pointer not being in the heap. Any help would be appreacitaed!
#include <stdio.h>
void printString();
int main (int argc, char* argv[]) {
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, &argv);
}
return 0;
}
void printString(int commandAmount, char* argv[]) {
printf("the word is %s," , argv[commandAmount]);
}
./shortExample example
Segmentation fault (core dumped)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
原型
void printstring();
与实际实现不匹配。应该是:
您还可以跳过原型,然后在
main
之前实现该功能。您的循环while(CommandAmount&lt; argc)
似乎没有任何方法可以完成,因为您永远不会增加CommandAmount
。这可能导致不确定的行为,因此您的程序可能会崩溃或做任何事情。我建议为 -loop做一个
来解决这个问题。
示例:
或以您结构的方式:
The prototype
void printString();
does not match the actual implementation.It should have been:
You could also skip the prototype and just implement the function before
main
. Your loopwhile (commandAmount < argc)
seems to not have any way to finish since you never increasecommandAmount
. This can cause undefined behavior and with such, your program may crash or do just about anything.I suggest making a
for
-loop to fix that.Example:
or in the way you structured it:
对于初学者,此功能声明
不提供函数原型。 编译器从函数调用确定函数参数的
因此,
类型标识符的声明
argv
,但是函数定义中的相应参数
printString
具有typechar **
,因为编译器参数调整了将数组类型用于指针到数组元素类型。也就是说,该函数声明
由编译器调整为
参数表达式和参数的不兼容类型。结果,此呼叫
调用了未定义的行为。
此外,总体而言,此循环通常
是无限循环,因为变量
CommandAmount
在循环中没有更改。首先,在
main
之前,您应该提供功能原型,以使您的程序更安全,并将功能调用
当然还需要更改Main中的循环。
请注意这一点,因为参数的值
CommandAmount
未在功能中输出,因此实际上是多余的。您可以将指针传递给字符串本身的功能。例如For starters this function declaration
does not provide a function prototype. So the compiler determines the type of the parameters of the function from the function call
However the expression
&argv
used in this callhas the type
char ***
due to the declaration of the identifierargv
But the corresponding parameter in the definition of the function
printString
has the typechar **
due to adjusting by the compiler parameters having array types to pointers to array element type.That is this function declaration
is adjusted by the compiler to
Thus there are incompatible types of the argument expression and of the parameter. As a result this call
invokes undefined behavior.
Moreover this loop in main
in general is an infinite loop because the variable
commandAmount
is not changed within the loop.Firstly you should provide the function prototype before
main
to make your program more saferand call the function like
Of course you need also to change the loop in main.
Pay attention to that as the value of the parameter
commandAmount
is not outputted within the function then in fact it is redundant. You could pass to the function the pointer to the string itself. For example