[haiku-development] converting const char* to char*

  • From: Suhel Mehta <mehtasuhel@xxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Fri, 17 Jul 2020 22:37:01 +0530

Hi Everyone!
While implementing code to read the directories of the UFS2 file
system I ran into a problem. I have a const char* variable that stores
a directory name and now I want to pass it as a parameter to the
GetNext function that accepts char*. After searching and testing I
have found the below code that may work

char* dir_name = new char[length + 1]();
strncpy(dir_name, name, length);

Here name is a const char* variable and length is an integer variable
with value of number of characters in the name variable.
but it is not working as expected, because in some cases it takes one
more character in dir_name whereas in some cases it works fine. Please
help me to solve the issue.

-- 
Suhel Mehta
https://github.com/SuhelMehta9

Other related posts: