The question is pretty straightforward. How do I convert a char[] to a char* in C?
I’m really having a hard time finding the solution for it. So your help is highly appreciated 🙂
Here is why I need this:
sprintf(name,"%c%c%%HACKED%c%c.virus",a,b,c,d); // print a formatted text to string (char[])
strcat(buffer,currentPath); // currentPath is of data type char[]
strcat(buffer,"\\");
strcat(buffer,name); // Now this is where the problem comes in. Since strcat function needs a const char* for it's second argument.
printf("%s",name);
You don’t have to do anything. The array automatically decays into the pointer, so your code appears fine.
You do need to make sure that
bufferstarts off with a valid, NUL-terminated string before you callstrcat()for the first time. It also needs to be writeable (it can’t, for example, be a pointer to a string literal).You also need to make sure you don’t overflow
buffer. I’d recommend you look intostrncat().