It depends on what you want to do, but what you are trying to do is not good. Instead, you could do — for example — this:
#include <cstring>
strcpy(stu[1].name, "ali souri");
strcpy(stu[2].name, "moh jdhfu");
strcpy(stu[3].name, "far nfjgi");
In this case, you would copy character data, but not the pointers, which would be really bad in this case.
Here is why: the strings on the right of assignment operator are
immediate constants, the string data could be stored right in the code (and usually stored this way), and the code is read-only, protected from modification on the CPU level. An attempt to write in this area would produce a general protection fault (in Intel CPUs). An assignment of a constant pointer to a non-constant pointer would open the write access to the read-only area in the memory, so you could, say, assign a different value to any element of the array
sty[1].name
, with the fatal result.
Yes, C++ has the constant cast operator (please see
http://en.cppreference.com/w/cpp/language/const_cast[
^]), but this is not what you would want to do, by the serious reason I've explained above.
[EDIT]
By the way, is there a specific reason to use null-terminated strings, not
std::string
? It would be safe, reliable, easy, more in the spirit of C++…
—SA