one question - Programmers Heaven

Howdy, Stranger!

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

Categories

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.