1

Whenever I send in a number from the command line it errors and gives me a wrong number

edgeWidth=*argv[2];
printf("Border of %d pixels\n", edgeWidth);
fileLocation=3;

./hw3 -e 100 baboon.ascii.pgm is what I send in through the command line and when I print the number to the screen I get 49 as the number

int edgeWidth is defined at the beginning of the program.

Why is it not giving me 100?

3 Answers 3

3

argv contains an array of strings. So argv[1] is a string, you need to convert it to an integer:

edgeWidth = atoi(argv[1]);
Sign up to request clarification or add additional context in comments.

Comments

2

The problem is that by doing

edgeWidth = *argv[2];

you're assigning the first character of "100" to edgeWidth. 49 happens to be the ASCII value for '1'.

If you want 100, you need to use something like atoi or strtol to parse the string into an int.


Addendum: Regarding numeric promotion, part two of 6.5.16.1 in the C99 spec states:

In simple assignment (=), the value of the right operand is converted to the type of the assignment expression and replaces the value stored in the object designated by the left operand.

so it does appear that numeric promotion happens here.

1 Comment

OK, I had a feeling that was the problem. Thank you it worked! Much appreciated
1

Because command line arguments are by default as char* (or may be char** somewhere) not int. you need proper conversion like atoi() to use it as int. You should use edgeWidth = atoi(argv[2]) to get expected output.

1 Comment

Wrong - use atoi(argv[2]) not atoi(*argv[2]). You would end up looking at a character instead of a string. I don't think the compiler would let you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.