how is size of pointer calculated?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ycoerohan
    New Member
    • Jan 2010
    • 5

    #1

    how is size of pointer calculated?

    char *ptr;

    sizeof(*ptr) will return you 1 (size of char varaible whose address it can hold).

    char *ptr[10];

    sizeof(*ptr) will return you 4 why???

    Please clear my doubt regarding pointer
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    char *ptr[10] is an array of 10 pointers to char.

    *ptr, as with any array is equilivent to the first member of the array, ptr[0]. Since ptr is an array of 10 pointers to char ptr[0] is a pointer to char (char*) so *ptr is a pointer to char (char*).

    Therefore sizeof(*ptr) == sizeof(ptr[0]) == sizeof(char *) which on you machine (which I assume is a 32bit machine) is 4 bytes.

    Comment

    Working...