Python verwöhnte mich und versuchte, mich um C zu wickeln, jetzt ist ein Blutbad von dummen Fehlern. Das kann ich nicht ganz verstehen.Warum haben diese Variablen den Typ scheinbar geändert?
Ich wollte das C-Äquivalent von Python os.path.split
, aber es gibt keine genaue Entsprechung. strsep
sieht ähnlich aus, aber muss massiert werden, um einfach verwendet zu werden.
Als erstes habe ich meinen Pfadtyp definiert: eine Zeichenfolge mit gegebener Länge.
Dann schrieb ich etwas Code, der die eigentliche Massage tut, versucht, Nebenwirkungen zu vermeiden - nur um die Dinge narrensicher zu halten.
typedef struct {
t_path next;
t_path remainder;
} t_path_next
t_path_next path_walk_into(t_path path) {
t_path_next output;
t_path my_next, my_remainder = "/";
strncpy(my_next, path, MAX_PATH_LEN);
strsep(&my_next, my_remainder);
output.remainder = my_remainder;
output.next = my_next;
return output;
}
GCC ist jedoch nicht beeindruckt.
[email protected]:~/blah$ gcc path.c -Wall
path.c: In function ‘path_walk_into’:
path.c:39: warning: passing argument 1 of ‘strsep’ from incompatible pointer type
/usr/include/string.h:559: note: expected ‘char ** __restrict__’ but argument is of type ‘char (*)[200]’
path.c:41: error: incompatible types when assigning to type ‘t_path’ from type ‘char *’
path.c:42: error: incompatible types when assigning to type ‘t_path’ from type ‘char *’
ich von der Notiz bin ratlos - wie geht es char **
und char (*)[200]
wirklich anders - aber der Fehler ist noch seltsamer. Ich möchte eine Variable zuweisen, die ich t_path
in einem Feld vom Typ t_path
deklariert habe, aber ich komme nicht dazu.
Warum ist das?
Für alle Interesse hier ist das richtig funktionierende Version der Funktion:
t_path_next path_walk_into(t_path path) {
t_path_next output;
t_path my_path, delim = "/";
char* my_path_ptr = my_path;
strncpy(my_path, path, MAX_PATH_LEN);
strsep(&my_path_ptr, delim); //put a \0 on next slash and advance pointer there.
if (my_path_ptr == NULL) //no more slashes.
output.remainder[0] = 0;
else
strncpy(output.remainder, my_path_ptr, MAX_PATH_LEN);
strncpy(output.next, my_path, MAX_PATH_LEN);
return output;
}
Ein Teil des Problems ist, dass in C, Arrays zu Zeigern in verschiedenen Situationen zerfallen. Wenn Sie versuchen, 'char *' anstelle von 'char [...] ', dann können Sie bessere Ergebnisse erzielen. Ich habe es jedoch nicht ausprobiert :) –
Die Verwendung von 'typedef' mit einem Array-Typ ist eine sichere Formel oder macht jeden, der jemals Ihren Code liest, wütend und versucht herauszufinden, was WTF macht. Tu das einfach nicht. –
Wenn der vorherige Kommentar Sie fragen ließ (es ließ mich wundern), hier ist [eine Erklärung] (http://stackoverflow.com/questions/4523497/typedef-fixed-length-array/4523537#4523537). Es sieht für mich nicht besonders stark aus (der Code wird immer noch korrekt funktionieren), aber es ist immer noch gültig. – badp