How to get the sizeof char pointer

The below code throws the error, since the size of x = 19 is not passed to the cstrCopy function.

using namespace std;
static void cstrCopy(char x, const chary);
int main ()
{
char x[19];
const string y = "UNIX FORUM";
cstrCopy(x,y.c_str());
return 0;
}
void cstrCopy(char *x, const char *y)
{
if(strlen(y) >= sizeof(x)) {
throw errorException;
}
strncpy(x, y, sizeof(x));
}

How to pass the size of char pointer to a function.?
Please help.

First, please use

where applicable
using namespace std;
static void cstrCopy(char *x, const char *y);
int main()
{
char x[19];
const string y = "UNIX FORUM";
cstrCopy(x, y.c_str());
return 0;
}
void cstrCopy(char *x, const char *y)
{
if (strlen(y) >= sizeof(x)) {
throw errorException;
}
strncpy(x, y, sizeof(x));
}[/code]
Second, a C "string" has no information about itself. It's just an array of characters, which is terminated by a null byte by convention. This also means that your two calls to sizeof will always return 4 (size of a pointer). Better add another parameter with the size of the array.
A more intelligent solution would be to malloc() the "C string" in your function, strncpy() the contents of y, and return the pointer to that memory area.

To clarify, from ISO C99 Sect 7.1.1, a string is a "contiguous sequence of characters terminated by and including the rst null character".

This is only true if you are on a platform whose programming model is such that a pointer is 32 bits. One such common programming model is ILP32 (Microsoft Windows, 32-bit Linux) where the size of an integer, long and pointer are all 32 bits. Another common programming model is LP64 (64-bit Linux) where the size of an integer is 32 bits but the size of a long and a pointer is 64 bits.

You should not be using sizeof to determine string lengths. Use strlen for that. Your sizeof is returning the correct size (4 or 8 depending on your system's architecture).