Thread: difference between char *var and char var[]

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    difference between char *var and char var[]

    Hey,

    If I'm not mistaken it is possible to specify both ways and they are identical
    although is this valid for assignments of variables later?

    for example:

    char var[] = "test";
    var = "testing more options";

    would this break?
    when var[] initializes it reserves enough memory, as much as "test" but later a longer string is evaluated to var, isn't this bad?


    thanks.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by liri View Post
    would this break?
    Yes, in fact, it wouldn't even compile...
    Code:
    char var1[80];  //I have enough memory for 80 chars
    char *var2;      //I hold the address to a char somewhere
    You cannot assign a string in quotes to a char array, you'll need to use snprintf.
    Note that var2 has absolutely *no* memory allocated whatsoever (except for itself). So trying to store a string in var2 will crash your program. Var2 *points* to a bit of memory. To use var2, it either has to point to some other char array (like var1) or to memory that you have allocated on the heap.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by liri View Post
    Hey,

    If I'm not mistaken it is possible to specify both ways and they are identical
    although is this valid for assignments of variables later?

    for example:

    char var[] = "test";
    var = "testing more options";

    would this break?
    when var[] initializes it reserves enough memory, as much as "test" but later a longer string is evaluated to var, isn't this bad?


    thanks.
    The compiler should not allow this.

    The difference is that char *var = "test" will get the address of a constant string stored somewhere in your executable file, char var[] = "test" will make some memory available that is filled in with "test". The latter can't be assigned a different value (but you can modify the content, and if you want to use strcpy() to fill it in, then you can do that). On the other hand, the pointer form can easily be changed to point to a different string, but if it's a string constant, it shouldn't be modified.

    If you want to do the above and be able to modify the string too, then I would suggest this:
    Code:
    char var[100] = "test";
    ...
      strcpy(var, "testing more options");
    ...
    If all you want is to have different strings depending on some condition, and don't plan to modify the string, you can use char *var, it would be the better approach.

    --
    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.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    13

    thanks for the reply

    if choosing to work with the case of char var[] and doing

    char var[] = "test"
    // and then wanting to zero the variable like var = "" which is not appropriate for [] but rather // for the pointer decleration. in this case would that be the same as doing
    strcpy(var, "");


    regards,
    liri.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you do char var[] = "", then you get an array of a single byte, which is quite useless.

    If you really want to do that, you should give a length.

    --
    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.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Yes, in theory it is. But normally if you use the "" initialiser with empty brackets, you're basically saying to anyone reading your code: "This variable is going to hold "test" and nothing else". If you then re-assign it, it's going to confuse a lot of people.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    13
    Well, I'm talking about converting this form to character array:
    char *var = "test";
    ...code...
    var = "";

    so doing that with strcpy(var, "") seems like the normal way of doing it.
    unless you can propose something else


    regards.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by liri View Post
    Well, I'm talking about converting this form to character array:
    char *var = "test";
    ...code...
    var = "";

    so doing that with strcpy(var, "") seems like the normal way of doing it.
    unless you can propose something else


    regards.
    That is what you'd have to do - but as QuantumPete says - it is not really how one would recommend doing it.

    The way I would recommend doing it is something like this:
    Code:
       char var[10];  // Suitable length should be choosen based on what you need. 
       strcpy(var, "");
    ...code...
       if (somecondition)
          strcpy(var, "Text");
    ....
       if (othercondition)
          strcpy(var, ...);
    [Using strncpy or snprintf may be better choices, as those are "safe" to prevent the string from being too long.]

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM