Thread: char *name?

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    char *name?

    What does the asterix after the char identifier which is apart of the variable name mean?

    Ex. char *file = "something";

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It means you're declaring a pointer, in this case a pointer initialized to a string located in read-only memory.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In that context it makes file a pointer to char. But the asterisk isn't really a part of the variable name. The same pointer could be written,
    Code:
    char* file;
    which is the same as
    Code:
    char * file;
    which is also
    Code:
    char *file;
    A lot of people will tell you that the asterisk is just there to remind you that file is a pointer, which is true, until you decide to use the dereference operator to get the value stored in memory:
    Code:
    *file = "something new";
    Last edited by whiteflags; 05-04-2006 at 02:20 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's similar, but not the same as, an array.
    Code:
    const char *progname = "program";
    char progname2[] = "program";
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User DavidG's Avatar
    Join Date
    Mar 2006
    Location
    England
    Posts
    13
    To put it simply(ish), a pointer variable isn't a char, or int, or whatever it's pointing to - it just holds the memory address of whatever it's pointing to. (As an unsigned integer, I believe. Though that doesn't make much difference unless you want to alter it or put it into a printf statement.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM