Can we typecast a string into a character array?

Can we typecast a string into a char array, if yes how can we do that and what is the difference between char array and char* . why char* accepts the whole string. for ex : char* a = “This is a good day.”

Hey @mukuuu
To discuss about difference in char* and char[] , let’s consider these examples:
char *p = “cats”;
char a[10] = “cats”;
Now a is an array and p is a pointer variable.
sizeof(a) = 10 bytes
sizeof( p) = 4 bytes
now for the pointer
char *p = “cats”;
you can make pointer point to another string like
p = “hello” is valid, but you can’t edit the character at a specific index, like p[0] = ‘t’ is invalid.
also, For char a[10] = “cats”
You cannot change the complete character just like you can assign the new string to the pointer
i.e. you can’t write a = “hello”
but you can change a particular element of an array write a[0] = ‘t’.

Also @mukuuu
You can’t exactly typecast but you can convert a string into a character array, basically you can copy contents of a string into a character array:
A way to do this is to copy the contents of the string to char array. This can be done with the help of c_str() and strcpy() function.
The c_str() function is used to return a pointer to an array that contains a null terminated sequence of character representing the current value of the string.

#include <bits/stdc++.h>` 

using namespace std; 

// driver code 
int main() 
{ 
	// assigning value to string s 
	string s = "coding"; 

	int n = s.length(); 

	// declaring character array 
	char char_array[n + 1]; 

	// copying the contents of the 
	// string to char array 
	strcpy(char_array, s.c_str()); 

	for (int i = 0; i < n; i++) 
		cout << char_array[i]; 

	return 0; 
} 

Output:
coding