Click here to Skip to main content
15,908,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What does this mean?

C#
char const *  pWhiteList[] =
{
    "str1",
    "str2",
};


I know what char * means.

I also know what char someVal[50] means.

But I have never seen them defined together, like above.

Any explanations welcome.

PS. I think similar construct one encounters in main in C++, e.g.,

C++
int _tmain(int argc, _TCHAR* argv[])


Again above we have this combination of char* and []:
C++
_TCHAR* argv[]
.
So what does this mean?
Posted

C++
char const *  pWhiteList[] =

Reading right to left:
pWhiteList[] : is an array
*            : of pointers
const        : to constant
char         : characters
 
Share this answer
 
It declares an array of constant pointer-to-characters and initialises it.
In other words it creates an array of two strings, and assigns it to pWhiteList.
 
Share this answer
 
Comments
Philippe Mori 11-Oct-13 20:42pm    
In fact, a pointer to constant char. To be a const pointer, const would have to be after the *.
Reading C/C++ declaration may be hard.
I had my troubles too until lately I learned to read these reliably.

Start at the entity name (e.g. x) and do the following:

  1. "declare x as "
  2. for all the directly following (...) and [...] say "function (...) returning " and "array [...] of " respectively
  3. for all directly prefixing pointer operators (e.g. *) say "pointer to " (add const, volatile and other pointer operators)
  4. if the above was nested (e.g. (*x)) continue a 2.
  5. what remains is the type, so you say "type"


E.g.

int xdeclare x as int
int *xdeclare x as pointer to int
const char *x[4]declare x as array [4] of pointer to const char
const char (*x)[4]declare x as pointer to array [4] of const char
long *f(double)declare x as function (double) returning pointer to long
long (*f)(double)declare x as pointer to function (double) returning long


Cheers
Andi
 
Share this answer
 
v3
Adding one more...

I think you may be confused by the layout, rewrite it like this and see if it makes more sense:

C++
char const *  pWhiteList[] = { "str1", "str2" };


For some reason they have it laid out like they would if declaring a struct or typedef, but its just a const pointer array to a couple strings.
 
Share this answer
 
Comments
_coder 17-Oct-13 4:13am    
so pWhiteList[0] is actually a pointer right?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900