Reputation: 165
1. char *names[5]={"Web","Security","Software","Hello","Language"};
2. char names[5][30]={"Web","Security","Software","Hello","Language"};
what is the difference between these two ?
One I know is that (1.) first one can have desired length of string while (2.) second one can have string 29 characters with '\0'
But I am confused that what's the difference when they are passed to function and how they are passed ?
Please elaborate I am new to C++ ....
Upvotes: 0
Views: 371
Reputation: 66371
The first one shouldn't compile unless you add a const
; const char *names[5] = ...
.
Once you fix that, the first is an array of pointers, the second is an array of arrays.
If you pass them to a function, the first will decay into a pointer to a pointer, const char**
, while the second will decay into a pointer to an array with 30 elements, char(*)[30]
.
That is,
void pointers(const char**);
void arrays(char(*)[30]);
const char *names[5]={"Web","Security","Software","Hello","Language"};
pointers(names); // Good
arrays(names); // Bad
char names[5][30]={"Web","Security","Software","Hello","Language"};
pointers(names); // Bad
arrays(names); // Good
Upvotes: 3