atoi() and *argv[]

This is a discussion on atoi() and *argv[] within the C Programming forums, part of the General Programming Boards category; So I have no idea how to make atoi() work in my program right about now. I have a function ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    44

    atoi() and *argv[]

    So I have no idea how to make atoi() work in my program right about now. I have a function written that does exactly what I want atoi() to do, but the professor insists on the use of atoi() instead of our own functions.

    Code:
    #include <stdio.h>
    
    int len(char *a);
    
    int main(int argc, char *argv[])
    {
            int atoi(const char *nptr);
            int i;
    
            printf("Output of strlen: %d \n",argv[2]);
    
            for(i = 1; i < argc; i++)
            {
    /*              printf("Output of strlen: %d \n",len(argv[i]));*/
            }
    
            return 0;
    }
    The output is always 0. I know it means that the input string cannot be converted, but I had this issue even when I just wrote something like atoi("jdghlgjsdh"). I have not included the code for the function len.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Where are you calling atoi() ?
    You just have a local prototype declaration in main.

    > printf("Output of strlen: %d \n",argv[2]);
    If you type
    myprog.exe param
    you will get 0 (argv[2] will be NULL)

    If you type
    myprog.exe param param
    then who knows what large number will be printed.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    argv[] is an array of pointers to the command-line arguments. So if you were to invoke the program by typing this into a terminal...

    Code:
    ./prog.exe hello world
    argv[0] == "./prog.exe"
    argv[1] == "hello"
    argv[2] == "world"
    argv[3] == NULL
    argv[4] == NULL

    and so on. As for atoi, this function takes a string as input and an integer as output. Consider the following.

    Code:
    char *x = "123";
    int y = atoi(x);
    This would assign y to the value of 123. If you input NULL, the function will output NULL.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    Oh sorry Salem it should be
    Code:
    printf("Output of strlen: %d \n",atoi(argv[2]));
    Bab, I need to to work with actual letters as well as numbers...that's what my problem is more than anything. Numbers work just fine for me, but I don't know how to make it work with letters.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    What do you think atoi() does?
    And why would you think it would do anything with a string of letters?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    That's probably my problem then. I did some more research, and apparently it doesn't do anything like what I thought. I was trying to use atoi() in a function that mimicked strlen().

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21