Thread: very unexpected values from *argv[]

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    10

    Question very unexpected values from *argv[]

    Hi
    Some code here, pushing values on a stack untill it finds an operator, and then does (it should) the operation. The first part pushing the values is working fine. Then, when it doesn't find a digit anymore and executes the switch arguments, it does something, for me, strange. When I output the values it recieves, instead operators (in ascii) it recieves letters, starting with several 'A's. I'm pretty sure to handle the casting/pointer for the switch input wrong, but nevertheless I can't explain the values, where they came from and why that many?
    --> given the input: exp 2 3 *

    Code:
    int main(int argc, char *argv[])
    {
        int n;
        double op2;
        while (--argc > 0) {
            if (isdigit(n = **++argv)) 
                push(atof(*argv));  // 
            else {
                switch (n) {
                    case '+':
                        push(pop() + pop());
                        break;
                    case '*':
                        push(pop() * pop());
                        break;
                    case '-':
                        op2 = pop();
                        push(pop() - op2);
                        break;
                    case '/':
                        op2 = pop();
                        if (op2 != 0.0)
                            push(pop() / op2);
                        else
                            printf("error: zero divisor\n");
                        break;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if (isdigit(n = **++argv))
    > push(atof(*argv)); //
    Try writing it out in long form, instead of a super-compressed embedded assignment and pointer arithmetic.

    IE, spend a few seconds more writing clear code, instead of spending days wondering why your "clever" code has out-smarted you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Remember that if you're passing characters special to the shell you need to escape them:
    $ calc 2 3 \*
    Otherwise you'll get a list of filenames instead (on linux at least)!

  4. #4
    Registered User
    Join Date
    Oct 2016
    Posts
    10
    Thank you 4 your responses!
    @Salem, that code never meant to be 'clever', I'm following the book "Programmieren in C" ("The C Programming Language") and use the techniques given at the time. But I understand what you mean, I will remember this to practise when I run into confusion, promise
    But have to admit, I really love that part of C (especially compared to python, which has also it's beauty) how you can embed assignments and incremention in conditions, it's beautiful
    @algorism You are right, thats the point!
    Used the same conditions earlier in the book I'm reading, where we grabbed the characters with getchar(), maybe I can use this again somehow. I'll see!
    HAve a beautiful day all!

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    10
    There seems no way around when using bash-command-line. But, well, that's okay, I see clear here now. Thank you very much, again! Yu are the best.
    --> I can't mark that thread as closed, can I ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-02-2013, 08:57 PM
  2. Unexpected change of input values
    By imrahaseen in forum C Programming
    Replies: 6
    Last Post: 06-22-2007, 02:25 AM
  3. Clearing unexpected values out of cin
    By IdioticCreation in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2006, 07:27 PM
  4. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM
  5. Unexpected results using argc / *argv[]
    By Morgan in forum C Programming
    Replies: 1
    Last Post: 09-11-2003, 01:38 AM

Tags for this Thread