开发者

Why am I getting a segmentation fault in this program?

开发者 https://www.devze.com 2023-01-23 15:14 出处:网络
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. However, I\'m getting a segmentation fault whenever I r

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. 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.

The code is supposed to work as follows:

./a.out -1,12

Prints:

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 开发者_StackOverflow社区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.

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);

should be

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


you should pass &a and &c to the function, for one.


You pass the int a and int c to the function that expects the int* a and int* c

instead of using

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

, try

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


You're mixing getnumber and getMandNvalues.

You've supplied a prototype for getnumber but no definition of that function. You supplied a definition of getMandNvalues but no prototype before you call this function.

Calling a function with no prototype in scope is legal. The compiler assumes it returns int and all arguments are int. Neither of these is true in this case.

Correct your prototype

0

精彩评论

暂无评论...
验证码 换一张
取 消