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("\nBeispiel:");
    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
    1. Advertising

  2. Zahpod

    Ian Collins Guest

    Zahpod wrote:
    >
    > for (i=1; i<argc; i++)
    > {
    > dec = stoi(argv);

    Should be atoi?

    --
    Ian Collins.
    Ian Collins, Apr 21, 2006
    #2
    1. Advertising

  3. Zahpod

    Richard Bos Guest

    "Zahpod" <> wrote:

    > #include <stdio.h>
    > #include <sys/ddi.h>


    This header does not exist in ISO C.

    > dec = stoi(argv);


    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
    Richard Bos, Apr 21, 2006
    #3
  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)

    NAME
    stoi, numtos - convert between an integer and a decimal
    string

    SYNOPSIS
    #include <sys/ddi.h>

    int stoi(char **str);

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

    INTERFACE LEVEL
    Solaris DDI specific (Solaris DDI).

    PARAMETERS
    str Pointer to a character string to be converted.

    num Decimal number to be converted to a character
    string.

    s Character buffer to hold converted decimal number.

    DESCRIPTION
    stoi()
    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
    examined.

    numtos()
    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
    #4
  5. Zahpod

    Zahpod Guest

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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Payne
    Replies:
    5
    Views:
    454
    William Payne
    Oct 12, 2004
  2. BORT
    Replies:
    38
    Views:
    1,313
    =?iso-8859-1?B?QW5kcuk=?=
    Jul 2, 2005
  3. Adriaan Renting
    Replies:
    1
    Views:
    326
    Roy Smith
    Jun 28, 2005
  4. baibaichen

    trivial or non-trivial object

    baibaichen, Jan 12, 2006, in forum: C++
    Replies:
    3
    Views:
    885
    osmium
    Jan 12, 2006
  5. Stefan Ram
    Replies:
    5
    Views:
    138
    Stefan Ram
    Mar 17, 2014
Loading...

Share This Page