how to use stoi? which *.h to use? trivial beginners problem...

Discussion in 'C Programming' started by Zahpod, Apr 21, 2006.

  1. Zahpod

    Zahpod Guest

    Hi all

    The Code:
    #include <stdio.h>
    #include <sys/ddi.h>

    main(int argc, char *argv[])
    int i,dec;
    if ( argc < 2 )
    printf("\nBitte eine oder mehrere Zahlen als Argument angeben!");
    printf("\n %s 10 1 12 132\n",argv[0]);
    return 2;

    for (i=1; i<argc; i++)
    dec = stoi(argv);
    printf("%s = %4d = %4x\n", argv, dec, dec);
    //printf("%s\n", argv);

    return 0;

    The problem:
    $ gcc dec2hex.c
    Undefined first referenced
    symbol in file
    stoi /var/tmp//ccepw0WS.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    Zahpod, Apr 21, 2006
    1. Advertisements

  2. Zahpod

    Ian Collins Guest

    Should be atoi?
    Ian Collins, Apr 21, 2006
    1. Advertisements

  3. Zahpod

    Richard Bos Guest

    This header does not exist in ISO C.

    Neither does this function.

    From context I suspect you meant the atoi() function, which is declared
    in <stdlib.h>. I suggest, however, that you use strtol() instead. It has
    better error handling, which can be important with data you get
    unfiltered from your user. For example, atoi() has undefined behaviour
    on overflow; strtol() does not. strtol() is also in <stdlib.h>.

    Richard Bos, Apr 21, 2006
  4. Zahpod

    Zahpod Guest

    1000x thanks, that was it!

    I mixed up stoi with atoi.

    Cause: Under Solaris 8 or 10 "man stoi" shows:

    Kernel Functions for Drivers stoi(9F)

    stoi, numtos - convert between an integer and a decimal

    #include <sys/ddi.h>

    int stoi(char **str);

    void numtos(unsigned long num, char *s);

    Solaris DDI specific (Solaris DDI).

    str Pointer to a character string to be converted.

    num Decimal number to be converted to a character

    s Character buffer to hold converted decimal number.

    stoi() returns the integer value of a string of decimal
    numeric characters beginning at **str. No overflow checking
    is done. *str is updated to point at the last character

    numtos() converts a long into a null-terminated character
    string. No bounds checking is done. The caller must ensure
    there is enough space to hold the result.
    Zahpod, Apr 21, 2006
  5. Zahpod

    Zahpod Guest

    Hi Richard
    Many thanks, your and Ian's answers helped!
    Have a nice weekend! :)
    Zahpod, Apr 21, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.