newbie Fragen zu malloc und sizeof

Kann mir jemand erklären, warum mein Aufruf von malloc mit einem string-in Größe 6 gibt eine sizeof von 4 Byte? In der Tat, jedes integer-argument gebe ich malloc bekomme ich eine sizeof von 4. Nächsten, ich bin versucht zu kopieren von zwei strings. Warum ist meine Ausgabe der kopierten Zeichenfolge (NULL)?
Folgendes ist mein code:

int main()
{
    char * str = "string";
    char * copy = malloc(sizeof(str) + 1);
    printf("bytes allocated for copy: %d\n", sizeof(copy));
    while(*str != '\0'){
        *copy = *str;
        str++;
        copy++;
    }
    copy = '\0';
    printf("%s\n", copy);
}
  • kopieren='\0' sein sollte *kopieren='\0'
InformationsquelleAutor John | 2009-10-07
Schreibe einen Kommentar