Python messed me up and trying to wrap my mind around C is now a bloodbath of stupid mistakes. This I can not understand.
I need the C-equivalent of Python os.path.split, but no exact equivalent. strsepLooks pretty similar, but it takes a little massage.
First, I defined my path type: a string of a given length.
#define MAX_PATH_LEN 200
typedef char t_path[MAX_PATH_LEN];
Then I wrote some code that does the actual massaging, trying to avoid side effects - just to keep things in the cold.
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, however, is not impressed.
badp@delta:~/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 *’
I am puzzled by the note - how different char **and char (*)[200], but the error is even more strange. I want to assign the variable I declared t_pathin the type field t_path, but I do not understand.
Why is this?
- :
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);
if (my_path_ptr == NULL)
output.remainder[0] = 0;
else
strncpy(output.remainder, my_path_ptr, MAX_PATH_LEN);
strncpy(output.next, my_path, MAX_PATH_LEN);
return output;
}