Waffles
Waffles

Reputation: 47

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

Upvotes: 0

Views: 274

Answers (5)

Hogan
Hogan

Reputation: 70523

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

Upvotes: 3

pmg
pmg

Reputation: 108978

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

Upvotes: 0

YYC
YYC

Reputation: 1802

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

Upvotes: 0

user472875
user472875

Reputation: 3175

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

Upvotes: 1

Armen Tsirunyan
Armen Tsirunyan

Reputation: 132994

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

should be

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

Upvotes: 2

Related Questions