Pointers are used to hold memory address of an another variable and point at it. But in this code a pointer array is initialized by string values that should be initialized in a normal array. Is this code valid ? If yes then can anyone please explain the details of using this type of code ? Like....its advantages or disadvantages if any, things that should be kept in mind while writing this and its purpose ? Thanks for any help.
Technically neither is valid, but for reasons outside of what you say.
In C++, string literals have type "const char*". You can't assign a const char* to a char* without a cast since it drops const-ness.
The correct declarations would be:
1 2
staticconstchar*[] = { "e1", "e2", "e3", "e4" }; // pardon my abbreviations
constchar* p = "Hello World";
The second declaration simply says that p is a pointer to a series of chars that form the string "Hello World". The compiler puts the string somewhere inside your executable, and p points to it. The first declaration simply declares an array of such things.
(So the only thing to keep in mind about all of this is that while you can change p to point to a different string, eg p = "Goodbye World", you cannot change what p is pointing to, eg *(p+1) = 'a'; /* will crash */