Portál AbcLinuxu, 27. dubna 2024 04:32


Dotaz: undefined reference to itoa ()

7.11.2006 21:50 xsustek | skóre: 6
undefined reference to itoa ()
Přečteno: 839×
Odpovědět | Admin
Prosim vas, mohol by mi niekto pomoct. Som uply zaciatocnik v C a mam problem ked kompilujem kod. Prekladac mi vypisuje undefined reference to itoa (). Pokilal som dobre pozeral tato funkcia by mala byt sucastou stdlib.h. Takze nechapem co ten linker potrebuje nalinkovat. Ak niekto bude vediet, mozte mi napisat aj ako to nalinkujem. Dakujem.
Nástroje: Začni sledovat (0) ?Zašle upozornění na váš email při vložení nového komentáře.

Odpovědi

7.11.2006 21:54 zabza | skóre: 52 | blog: Nad_sklenkou_cerveneho
Rozbalit Rozbalit vše Re: undefined reference to itoa ()
Odpovědět | | Sbalit | Link | Blokovat | Admin
funkce itoa() opravdu neexistuje...
7.11.2006 23:16 Michal Kubeček | skóre: 72 | Luštěnice
Rozbalit Rozbalit vše Re: undefined reference to itoa ()
Na některých platformách asi ano (nejspíš inverze k atoi()), ale ne v POSIXu ani v dalších rozšířeních glibc; tipoval bych, že jde o pokus přeložit program psaný původně pro nějakou jinou platformu. Takhle to dopadá, když se nepoužívá -Wall nebo se ignorují varování překladače.
8.11.2006 10:46 ams | skóre: 10
Rozbalit Rozbalit vše Re: undefined reference to itoa ()
Odpovědět | | Sbalit | Link | Blokovat | Admin
#include <stdio.h>
#include <string.h>

/*
Converts an integer value to a null-terminated string using 
the specified radix and stores the result in the given buffer.
If radix is 10 and value is negative the string is preceded by 
the minus sign (-). With any other radix, value is always considered 
unsigned. buffer should be large enough to contain any possible value: 
(sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms 
and 33 in 32-bits platforms.
Parameters:
 value    Value to be represented as a string. 
 buffer   Buffer where to store the resulting string. 
 radix    Numeral radix in which value has to be represented, between 2 and 36. 
Return Value:
 A pointer to the string. 
No error checking is performed.
*/
char* itoa (int value, char* buffer, int radix)
{
        if ( radix == 10 ) {
                sprintf (buffer, "%d", value);
        } else {
                char tmp_buffer[ sizeof(int)*8 + 1 ];
                char *ptr = &tmp_buffer[ sizeof(int)*8 ];
                unsigned n = value;
                *ptr-- = 0;
                do {
                        *ptr-- = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" [n % radix];
                        n /= radix;
                } while ( n );
                strcpy (buffer, ++ptr);
        }
}

int main ()
{
        int  value, radix;
        char buffer[sizeof(int)*8 + 1];
        while (1) {
                printf ("Value (decimal): "); scanf ("%d", &value);
                printf ("Radix (2 .. 36): "); scanf ("%d", &radix);
                if ( radix < 2 || radix > 36 ) {
                        printf("Good bye!\n");
                        return 0;
                }
                itoa (value, buffer, radix);
                printf("Result: %s\n", buffer);
        }
}
8.11.2006 10:46 ams | skóre: 10
Rozbalit Rozbalit vše Re: undefined reference to itoa ()
Odpovědět | | Sbalit | Link | Blokovat | Admin
#include <stdio.h>
#include <string.h>

/*
Converts an integer value to a null-terminated string using 
the specified radix and stores the result in the given buffer.
If radix is 10 and value is negative the string is preceded by 
the minus sign (-). With any other radix, value is always considered 
unsigned. buffer should be large enough to contain any possible value: 
(sizeof(int)*8+1) for radix=2, i.e. 17 bytes in 16-bits platforms 
and 33 in 32-bits platforms.
Parameters:
 value    Value to be represented as a string. 
 buffer   Buffer where to store the resulting string. 
 radix    Numeral radix in which value has to be represented, between 2 and 36. 
Return Value:
 A pointer to the string. 
No error checking is performed.
*/
char* itoa (int value, char* buffer, int radix)
{
        if ( radix == 10 ) {
                sprintf (buffer, "%d", value);
        } else {
                char tmp_buffer[ sizeof(int)*8 + 1 ];
                char *ptr = &tmp_buffer[ sizeof(int)*8 ];
                unsigned n = value;
                *ptr-- = 0;
                do {
                        *ptr-- = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" [n % radix];
                        n /= radix;
                } while ( n );
                strcpy (buffer, ++ptr);
        }
}

int main ()
{
        int  value, radix;
        char buffer[sizeof(int)*8 + 1];
        while (1) {
                printf ("Value (decimal): "); scanf ("%d", &value);
                printf ("Radix (2 .. 36): "); scanf ("%d", &radix);
                if ( radix < 2 || radix > 36 ) {
                        printf("Good bye!\n");
                        return 0;
                }
                itoa (value, buffer, radix);
                printf("Result: %s\n", buffer);
        }
}
8.11.2006 10:52 ams | skóre: 10
Rozbalit Rozbalit vše Re: undefined reference to itoa ()
Omlouvam se za 2-krat vlozeny prispevek. Chtel jsem vlozit upravenou verzi programu a nejak mi to odeslalo podruhe. Spravne by na konci funkce itoa melo stat return buffer;. Takze nejak takto:
                } while ( n );
                strcpy (buffer, ++ptr);
        }
        return buffer;
}
21.2.2007 19:20 nobody important
Rozbalit Rozbalit vše Re: undefined reference to itoa ()
Odpovědět | | Sbalit | Link | Blokovat | Admin
funce itoa() existuje

do hlavičky hezky

#include <stdlib.h>

a pak už jen

itoa(I,S,i)

kde I je vstupní integer; S výstupní string a i je soustava (2-dvojkova;8-osmičková;10-desítková;16-hexadecimální)
Luboš Doležel (Doli) avatar 21.2.2007 19:47 Luboš Doležel (Doli) | skóre: 98 | blog: Doliho blog | Kladensko
Rozbalit Rozbalit vše Re: undefined reference to itoa ()
$ grep itoa /usr/include/gentoo-multilib/amd64/stdlib.h
$
?
21.2.2007 22:04 Ash
Rozbalit Rozbalit vše Re: undefined reference to itoa ()

Založit nové vláknoNahoru

Tiskni Sdílej: Linkuj Jaggni to Vybrali.sme.sk Google Del.icio.us Facebook

ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.