confusion in array of pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nitishgupta
    New Member
    • Dec 2010
    • 1

    #1

    confusion in array of pointers

    char *name[]={"sachin","kap il","ajay"};
    cout<<name[0]

    what is the output of the above mentioned code?I think that
    output must be an address because name[0]is storing the base address of sachin.But i get sachin as output.why is it so?
    please help me!
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are correct. name[0] is an array. Therefore, he name of the array, name[0] in this case is the address of element 0 which is the "s" in "sachin. That is, name [0] is a char*.

    However, The << operator of cout assumes that when a char* is to be displayed that it is a string and that you want to see the strng and not the address of the string.

    To see the address of the string you will need to typecast name[0] to an int and then cout the int.

    Comment

    Working...