Wie cast-Programm-argument *char als int, richtig?

Bin ich auf einen Mac OS X 10.6.5 mit XCode 3.2.1 64-bit zu bauen, ein C command line tool mit einer build-Konfiguration 10.6 | Debug | x86_64. Ich wollen, eine positive gerade ganze Zahl als argument, also ich bin casting-index 1 argv als int. Dies scheint zu funktionieren, außer es scheint, dass mein Programm wird immer der ascii-Wert zu Lesen, anstatt die ganze char-array und Umwandlung in einen int-Wert. Wenn ich 'progname 10', sagt es mir, dass ich eingegeben habe 49, das ist, was ich auch bekomme, wenn ich enter "progname 1'. Wie bekomme ich C Lesen Sie die gesamte char-array als int-Wert? Google zeigte mir nur (int)*charPointer, aber klar, das funktioniert nicht.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(int argc, char **argv) {
    char *programName = getProgramName(argv[0]); //gets the name of itself as the executable
    if (argc < 2) {
        showUsage(programName);
        return 0;
    }
    int theNumber = (int)*argv[1];
    printf("Entered [%d]", theNumber);            //entering 10 or 1 outputs [49], entering 9 outputs [57]
    if (theNumber % 2 != 0 || theNumber < 1) {
        showUsage(programName);
        return 0;
    }

    ...

}

InformationsquelleAutor Rebecca Nelson | 2010-12-01

Schreibe einen Kommentar