--- In [email protected], "titli_juit" <[EMAIL PROTECTED]> wrote:
>
> char *p="asdaddaddA";
> printf("%d",sizeof(p));
>
> the ans is 2 or 4 depending on compiler. can u plzz expalin why it
> would be so? i think that the sizeof operator on a pointer type
> variable returns size of the type of object pointer points to.
sizeof returns the size of its argument; if it's a pointer, then it's
the size of the pointer (which might indeed be 2 on some 16 bit
systems, including the one I write code for). If you want the size of
what it's pointing to, then you have to do:
sizeof *p
But in this case, p is just pointing to a char, hence the result is 1.
You know it's the first of a string of chars, but C doesn't. You would
have to use strlen(p) (+ 1 if you want to include the \0 at the end of
the string) to get the result you want.
Alternatively, if you did:
char p[] = "asdaddaddA";
printf("%d", sizeof p);
p is then an array, and sizeof gives you its size (including the \0) = 11.
John