How to change the working directory in C?

2019-09-04 17:40发布

chdir can be used for constant character paths (it takes a const char *), but not for paths inputted by the user (since they have type char *). Is there any way around this?

标签: c linux chdir
2条回答
做自己的国王
2楼-- · 2019-09-04 18:17

chdir can be used with arbitrary string. const modifier means that it will not modify your string.

查看更多
孤傲高冷的网名
3楼-- · 2019-09-04 18:26

To expand on Roman Cheplyaka's answer, type qualifiers can always be added to pointer types, but not removed. This means that a function that takes a const char * parameter is really saying it can take either a char * or a const char * (which effectively means it is promising not to alter the contents of the string).

Similarly, a function that has a volatile char * parameter can take either a char * or a volatile char * as the actual argument, and a function that takes a const volatile char * parameter can take any of char *, const char *, volatile char *, or const volatile char *.

查看更多
登录 后发表回答