Thread: char * x, char x[]

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    char * x, char x[]

    hi pals,

    can any body explain the difference between
    char *x = "criticalboot";
    char x[] = "criticalboot";

    thnx in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The first stores the address of a constant string that is held somewhere else in your code.
    The second creates a space large enough to hold your string [including the final zero], and stores the string in that space.

    In old times, it would make little difference, but in modern compilers, the first form is read only, and should therefore be
    Code:
    const char *x = "criticalboot";
    so that you can't accidentally write code that tries to modify the value.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    thx

    Thanks Mats its help me a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM