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


Z

Zahpod

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
 
Ad

Advertisements

R

Richard Bos

Zahpod said:
#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
 
Z

Zahpod

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.
....
....
 
Ad

Advertisements


Top