简体   繁体   English

为什么在此程序中出现分段错误?

[英]Why am I getting a segmentation fault in this program?

I'm trying to set the values of M and N in this program to whatever is parsed from a string that this C program receives on it's command line. 我正在尝试将此程序中的M和N的值设置为从此C程序在其命令行上接收的字符串解析的任何值。 However, I'm getting a segmentation fault whenever I run the code. 但是,每当我运行代码时,我都会遇到分段错误。 I'm new to the concept of pointer in C, so I know it's something there. 我是C语言中的指针概念的新手,所以我知道它的存在。

The code is supposed to work as follows: 该代码应按以下方式工作:

./a.out -1,12 ./a.out -1,12

Prints: 印刷品:

1, 12 1、12

Thanks for any help! 谢谢你的帮助!

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

    void getnumber(char *toTest, int *a, int *c);

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

            int a, c, curr; 

            a = 1;
            c = 1;
            curr = 1;

            if ( argv[1][0] == '-' )
            {
                    curr = 2;
                    getMandNValues(argv[1], &a, &c);
            }

            printf("%d, %d\n", a, c);
            return 0;
    }


    void getMandNValues(char *src, int *a, int *c)
    {

            char aString[sizeof src];
            char bString[sizeof src];

            int i = 0;

            while((aString[i] = &src[i+1]) != ',')
                    ++i;

            aString[i] = '\0';

            int j = 0;

            while((bString[j] = &src[i + 2]) != '\0')
            {
                    ++j;
                    ++i;
            }

            bString[j] = '\0';

            *a = atoi(aString);
            *c = atoi(bString);
    }

The compiler output is: 编译器输出为:

/tmp/foo.c: In function ‘main’:
/tmp/foo.c:18: warning: passing argument 2 of ‘getMandNValues’ makes pointer from integer without a cast
/tmp/foo.c:18: warning: passing argument 3 of ‘getMandNValues’ makes pointer from integer without a cast
/tmp/foo.c: In function ‘getMandNValues’:
/tmp/foo.c:34: warning: assignment makes integer from pointer without a cast
/tmp/foo.c:41: warning: assignment makes integer from pointer without a cast

Did not look at everything but you need the address of the vars for this call. 并没有查看所有内容,但您需要此调用的var地址。

getMandNValues(argv[1], &a, &c);

I don't know what compiler you are using but I would not ignore the warning it must have displayed at compile. 我不知道您使用的是什么编译器,但我不会忽略它在编译时必须显示的警告。 (If you are not using the highest level of warning you should.) (如果未使用最高级别的警告,则应使用。)


Looking some more there is another problem 看更多还有另一个问题

while((aString[i] = &src[i+1]) != ',')
   ++i;

Seems strange (and wrong). 似乎很奇怪(和错误)。 I would do this: 我会这样做:

int index=0;
do
{
  aString[index] = src[index+1];
  index++;
} while (str[index] != ',')

here is another problem 这是另一个问题

char aString[len(src)];
char bString[len(src)];
getMandNValues(argv[1], a, c);

应该

getMandNValues(argv[1], &a, &c);

您应该将&a和&c传递给该函数。

You're mixing getnumber and getMandNvalues . 您正在混合getnumbergetMandNvalues

You've supplied a prototype for getnumber but no definition of that function. 您已经为getnumber提供了一个原型,但是没有该函数的定义。 You supplied a definition of getMandNvalues but no prototype before you call this function. 在调用此函数之前,您提供了getMandNvalues的定义,但没有提供原型。

Calling a function with no prototype in scope is legal. 调用范围内没有原型的函数是合法的。 The compiler assumes it returns int and all arguments are int . 编译器假定它返回int并且所有参数都是int Neither of these is true in this case. 在这种情况下,这些都不是正确的。

Correct your prototype 更正您的原型

You pass the int a and int c to the function that expects the int* a and int* c 您将int a和int c传递给需要int * a和int * c的函数

instead of using 而不是使用

getMandNValues(argv[1], a, c);

, try ,尝试

getMandNValues(argv[1], &a, &c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM