c-分割故障错误是由于调用用户在终端中输入的文本的函数而引起的

发布于 2025-02-07 22:51:40 字数 588 浏览 2 评论 0原文

摘要

我正在尝试传递用户在终端中放置的文本,并将其传递到名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落墨 2025-02-14 22:51:40

原型void printstring();与实际实现不匹配。
应该是:

void printString(int commandAmount, char* argv[]);

您还可以跳过原型,然后在main之前实现该功能。您的循环while(CommandAmount&lt; argc)似乎没有任何方法可以完成,因为您永远不会增加CommandAmount。这可能导致不确定的行为,因此您的程序可能会崩溃或做任何事情。
我建议为 -loop做一个来解决这个问题。

示例:

#include <stdio.h>

void printString(int commandAmount, char* argv[]) {
    printf("the word is %s,", argv[commandAmount]);
}

int main(int argc, char* argv[]) {
    for(int commandAmount = 1; commandAmount < argc; ++commandAmount) {
        printString(commandAmount, argv);
    }
}

或以您结构的方式:

#include <stdio.h>

void printString(int commandAmount, char* argv[]); // corrected

int main(int argc, char* argv[]) {
    int commandAmount = 1;
    while (commandAmount < argc) {
        printString(commandAmount, argv);
        ++commandAmount;                           // needed
    }
}

void printString(int commandAmount, char* argv[]) {
    printf("the word is %s,", argv[commandAmount]);
}

The prototype void printString(); does not match the actual implementation.
It should have been:

void printString(int commandAmount, char* argv[]);

You could also skip the prototype and just implement the function before main. Your loop while (commandAmount < argc) seems to not have any way to finish since you never increase commandAmount. 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:

#include <stdio.h>

void printString(int commandAmount, char* argv[]) {
    printf("the word is %s,", argv[commandAmount]);
}

int main(int argc, char* argv[]) {
    for(int commandAmount = 1; commandAmount < argc; ++commandAmount) {
        printString(commandAmount, argv);
    }
}

or in the way you structured it:

#include <stdio.h>

void printString(int commandAmount, char* argv[]); // corrected

int main(int argc, char* argv[]) {
    int commandAmount = 1;
    while (commandAmount < argc) {
        printString(commandAmount, argv);
        ++commandAmount;                           // needed
    }
}

void printString(int commandAmount, char* argv[]) {
    printf("the word is %s,", argv[commandAmount]);
}
倾其所爱 2025-02-14 22:51:40

对于初学者,此功能声明

void printString();

不提供函数原型。 编译器从函数调用确定函数参数的

printString(commandAmount, &argv);

因此,

printString(commandAmount, &argv);

类型标识符的声明argv

int main (int argc, char* argv[]) {
                    ^^^^^^^^^^^^

但是函数定义中的相应参数printString具有type char **,因为编译器参数调整了将数组类型用于指针到数组元素类型。

也就是说,该函数声明

void printString(int commandAmount, char* argv[]) {

由编译器调整为

void printString(int commandAmount, char** argv) {
                                    ^^^^^^^^^^^

参数表达式和参数的不兼容类型。结果,此呼叫

printf("the word is %s," , argv[commandAmount]);

调用了未定义的行为。

此外,总体而言,此循环通常

    int commandAmount = 1;
    while (commandAmount < argc) {
    printString(commandAmount, &argv);
    }

是无限循环,因为变量CommandAmount在循环中没有更改。

首先,在main之前,您应该提供功能原型,以使您的程序更安全

void printString(int commandAmount, char** argv);

,并将功能调用

printString(commandAmount, argv);
                           ^^^^

当然还需要更改Main中的循环。

请注意这一点,因为参数的值CommandAmount未在功能中输出,因此实际上是多余的。您可以将指针传递给字符串本身的功能。例如

#include <stdio.h>

void printString( const char *s );

int main( int argc, char* argv[] ) 
{
    for ( int commandAmount = 1; commandAmount < argc; commandAmount++ ) 
    {
        printString( argv[commandAmount] );
    }
    putchar( '\n' );

    return 0;
}

void printString( const char *s ) 
{
    printf( "the word is %s, " , s );
}

For starters this function declaration

void printString();

does not provide a function prototype. So the compiler determines the type of the parameters of the function from the function call

printString(commandAmount, &argv);

However the expression &argv used in this call

printString(commandAmount, &argv);

has the type char *** due to the declaration of the identifier argv

int main (int argc, char* argv[]) {
                    ^^^^^^^^^^^^

But the corresponding parameter in the definition of the function printString has the type char ** due to adjusting by the compiler parameters having array types to pointers to array element type.

That is this function declaration

void printString(int commandAmount, char* argv[]) {

is adjusted by the compiler to

void printString(int commandAmount, char** argv) {
                                    ^^^^^^^^^^^

Thus there are incompatible types of the argument expression and of the parameter. As a result this call

printf("the word is %s," , argv[commandAmount]);

invokes undefined behavior.

Moreover this loop in main

    int commandAmount = 1;
    while (commandAmount < argc) {
    printString(commandAmount, &argv);
    }

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 safer

void printString(int commandAmount, char** argv);

and call the function like

printString(commandAmount, argv);
                           ^^^^

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

#include <stdio.h>

void printString( const char *s );

int main( int argc, char* argv[] ) 
{
    for ( int commandAmount = 1; commandAmount < argc; commandAmount++ ) 
    {
        printString( argv[commandAmount] );
    }
    putchar( '\n' );

    return 0;
}

void printString( const char *s ) 
{
    printf( "the word is %s, " , s );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文