1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Starting a C program with a command

  1. Jun 14, 2017 #1
    1. The problem statement, all variables and given/known data
    Analyze the code below and figure out what it outputs if it's started with this command:
    kod.PNG
    The correct solution is given so i can better understand the parts of the problem.
    2. Relevant equations
    3. The attempt at a solution

    I had typed it out and tried to get the same result on Visual Studio 2017 by passing it's command to Visual Debugger Command line Argument and it keeps outputting
    output.PNG
    Maybe i did something wrong with the command line? I do not know. I continued to analyze it by hand and i had a few questions along the way that were stopping me from figuring it out. Here is fully typed out and annotated code in which i showed my understanding of it.
    Code (C):

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

       //./program.exe polisemija metonimija 0x1 2

    void f(char *a, char *b, int c) {
       if (a < b && c>0) f(a++, b--, c - 1);
       *a = *a < *b ? *a : ++*b;
    }

       //argc = 5 and argv start from 0 with argv[0] = ./program.exe and end with argv[4] = 2
    void main(int argc, char *argv[]) {
       int c = atoi(argv[argc - 1]);
           // c = 2;
       char *p = argv[atoi(argv[3]) + 1],
           //*p = argv[1 + 1] = "metonimija" but my debigger shows *p = "./program.exe"
           //how can that be?
           *q = p + strlen(p) - 1;
           //here q = p + 13 - 1; if we take *p = "./program.exe" but what is p here ?
           //my debugger gives q = 'e' so does this mean it points to 12th charachter of *p?
       f(p, q, c);
       printf("%s", p);
     
    I wanted to stop at this point to make sure my i typed in everything correctly as i do not get the right result. What is wrong here?
     
  2. jcsd
  3. Jun 14, 2017 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    What is the output of atoi(argv[3])? Maybe it has a problem with the hexadecimal number. If it returns -1, ...
    What a horrible code. Anyway, you can modify p to test your assumption.
     
  4. Jun 16, 2017 #3

    Mark44

    Staff: Mentor

    Minor point -- your code is missing the final right brace at the end. You must have not copied it, since your code actually compiles and runs.

    When you run your program, argc = 5, argv[0] contains the name of the program, argv[1] is "polsemija", argv[2] is "polisemija", argv[3] is "0x1" and argv[4] is "2".
    The expression atoi(argv[3] is 0, not 1 as you show in your comment. The reason is that atoi("0x1") evaluates to 0, since 'x' is not a decimal digit. (See http://www.cplusplus.com/reference/cstdlib/atoi/?kw=atoi for more information.)
    So argv[atoi(argv[3]) + 1] is argv[0 + 1] or argv[1], which is the string "polisemija", and p contains the address of that string. I don't know why your debugger shows a different string.
     
    Last edited: Jun 16, 2017
  5. Jun 16, 2017 #4
    I know, it's bugging me too but this is how the tests in my school are created. They tend to write purposely complicated and horrible code to make it harder for us to analyze it. Sad.
    1-st prog.PNG
    Is this where i was suppose to write the command argument? Maybe there is something wrong with this. Also here is a debugging screenshot:
    2-nd.jpg
    It shows that p is the first parameter of the argument but it should be the argv[1] = "polisemija" like you said and not this.
     
  6. Jun 16, 2017 #5

    Mark44

    Staff: Mentor

    Don't include the program name in the command arguments. The program name (and path) is already argv[0], so you're essentially getting it twice by making it argv[1].

    Also, the argv array should contain 5 values, not the 6 that you show.
     
    Last edited: Jun 16, 2017
  7. Jun 17, 2017 #6
    That was it! Without the program name it runs fine. But i cant still get the result by analyzing. Some thing aren't clear to me.
    Code (C):

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

       //./program.exe polisemija metonimija 0x1 2

    void f(char *a, char *b, int c) {
       if (a < b && c>0) {
           f(a++, b--, c - 1);
       }
       int t = *a < *b;
       *a = *a < *b ? *a : ++*b;
    }
       //here is the analysis of the void f function
       //the first thing that comes to my attention is a < b? How is a smaller than b?
       //since that is true we move on to do the function again passing a++,b--, c-1;
       //my debugger here shows only that c changed from c=2 to c=1 and no change in a or b. Why?
       //the loops finished and we now execute the last line of the fucntion 3 times
       //first time
       //*a = 0 ? *a : ++*b;  here *a = "olisemijb" and *b = "jb"
       //before i continue i need to understand how this changed.
       //++*b line was supposed to change "a" to "b" right?
       



    void main(int argc, char *argv[]) {
       int c = atoi(argv[argc - 1]);
           // c = 2;
       int number = atoi(argv[3]) + 1;
           //number = 1;

       char *p = argv[number], *q = p + strlen(p) - 1;
           //p = "polisemija" and q = a
           
       f(p, q, c);
       printf("%s", p);
    }
     
     
  8. Jun 17, 2017 #7

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    What is unclear?

    If it is q, see what happens if you change polisemija to polisemijb or poliserhrhorhemija, for example.
     
  9. Jun 17, 2017 #8

    Mark44

    Staff: Mentor

    One thing that is tricky in this program is that the function f is recursive, since it calls itself. Another thing that you might not be understanding is that the expression a < b is comparing addresses, not the characters at those addresses.
     
  10. Jun 17, 2017 #9
    Oh i did not know that. So a < b will always be true because the address of the firstly declared is always lower.

    I don't get what happens hen they do f(a++, b--, c -1). I know i enter the function again with the old values of a and b but since they are the addresses what happens when i decrement an address? What does a++ do? Also when i get to the *a < *b is a a pointer the the first letter? Does ++*b increment the value of the first letter then?
     
  11. Jun 17, 2017 #10

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    You point to the element one before it. In this case, the previous character in the string.
    Increasing a char should give the following letter in the alphabet (or something else from z and Z). Who does that?
     
  12. Jun 17, 2017 #11

    Mark44

    Staff: Mentor

    Not necessarily. If the variables are local to the function, and likely allocated on the stack, it's probably true that the last one declared has a lower address.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Starting a C program with a command
  1. Program in C (Replies: 1)

  2. C++ Program (Replies: 17)

Loading...