Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

one question

svazsvaz Posts: 2Member
what is the difference between char name[] and char *name:
can you please explain me with an example?

Comments

  • DonotaloDonotalo Posts: 715Member
    : what is the difference between char name[] and char *name:
    : can you please explain me with an example?
    :
    [purple]
    it depends how u r using name[] and *name; in the following context, these two are same:[code]
    char name[] = "First name";
    --------------------------
    char *name = "First name";
    [/code]
    in the declaration in function prototype, these two are also same:[code]
    void function(char *name);
    ---------------------------
    void function(char name[]);
    [/code]
    however, usually when char name[] is used, it means a static array, whose address cannot be changed. but when char *name is used, it can be point to any valid memory of the same type (ie, char here). look at the following code, where u cannot use [] and * interchangeably:
    [code]
    char name[] = "The string";
    char *ptr;
    for (ptr = name; *ptr; ptr++)
    cout << *ptr << '-';
    [/code]
    also, u cannot use pointer arithmatic with name, where as it is possible with ptr (as u see, ptr++).
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.