CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jun 2002
    Posts
    137

    Is this valid initialize of char []

    Hi,

    I see the initializition of a char array like this:

    Code:
    char ident[] = "@(#) You are running version "SOFTVERSION ;
    SOFTVERSION is a char *;

    Is this a valid char [] declaration & init for ASCII C standard?

    Thanks a lot in advance!

    sandodo

  2. #2
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    Re: Is this valid initialize of char []

    Quote Originally Posted by sandodo

    Code:
    char ident[] = "@(#) You are running version "SOFTVERSION ;
    SOFTVERSION is a char *;

    Is this a valid char [] declaration & init for ASCII C standard?
    No...

  3. #3
    Join Date
    May 2005
    Posts
    399

    Re: Is this valid initialize of char []

    sandodo: were you able to compile this succesfully?

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Is this valid initialize of char []

    Is SOFTVERSION a char * or a string literal?

    You are allowed to paste together two string literals.

  5. #5
    Join Date
    Jun 2002
    Posts
    137

    Re: Is this valid initialize of char []

    Sorry, the SOFTVERSION is actually a define

    Code:
    #define SOFTVERSION      "5.46"
    by the way, is it possible or isthere a way to do init a char* [] like this?

    Code:
    #define str "a string"
    
    char* dent[] = {"@(#)"str};  //or char* dent[] = {str};
    I am not quite sure what is the meaning of @#, can you please explain it?

    Thanks!
    sandodo

  6. #6
    Join Date
    May 2005
    Posts
    399

    Re: Is this valid initialize of char []

    If you do this

    Code:
    #include <iostream.h>
    
    #define str " a string"
    
    int main(int argc, char* argv[])
    {
    
      char* dent[] = {"test" str};
      cout << *dent << endl;
    
      return 0;
    }
    Then your output will be :
    Code:
    test a string
    @# is just an example of a string, you can substitute it with anything.

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Is this valid initialize of char []

    The preprocessor does its job before the actual compilation kicks in and hence that way of initialization is okay but why would you want to do that? Why don't you have file-scoped constant string literals. Regards.

  8. #8
    Join Date
    Jun 2002
    Posts
    137

    Re: Is this valid initialize of char []

    Thanks for all your replies.

    I think it is supposed to let programmer only modify the define of SOFTVERSION so that all other part of the codes know that now version changes to a new one.

    And I tried this for init of a array:

    Code:
    char * const_str = "a test";
    
    char* dent[] = {const_str};
    it doesnot work, is there a way to walk around without doing as following?:

    Code:
    char * const_str = "a test";
    
    char* dent[] = {0}; 
    
    dent[0] = const_str;
    Thanks!
    sandodo

  9. #9
    Join Date
    May 2005
    Posts
    151

    Re: Is this valid initialize of char []

    I'm not sure what you're trying to do with those curly braces, but they don't belong there. And dent[0] refers to a single character but const_str refers to an array of characters, so that assignment is invalid. I think what you want is simply:
    Code:
    const char * const_str = "a test";
    // ...
    const char * dent = const_str;

  10. #10
    Join Date
    Jun 2002
    Posts
    137

    Re: Is this valid initialize of char []

    no,
    Code:
    char* dent[] = {0};
    is declared as an array of char * and dent[0] is init to be NULL

    I am considering assign an array of char* to some char* variables passed into a function as arguments.

  11. #11
    Join Date
    May 2005
    Posts
    151

    Re: Is this valid initialize of char []

    Quote Originally Posted by sandodo
    no,
    Code:
    char* dent[] = {0};
    is declared as an array of char * and dent[0] is init to be NULL

    I am considering assign an array of char* to some char* variables passed into a function as arguments.
    Oops, my bad. You're right.

    So what's with the curly braces? It's legal of course, but it sure looks odd to me. What's the purpose?

  12. #12
    Join Date
    Dec 2005
    Posts
    642

    Re: Is this valid initialize of char []

    Quote Originally Posted by sandodo
    Code:
    char * const_str = "a test";
    
    char* dent[] = {const_str};
    it doesnot work, is there a way to walk around?
    Yes,

    Code:
    char const_str[] = "a test";
    
    char* dent[] = {const_str};

  13. #13
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: Is this valid initialize of char []

    char *const_str = "a test";
    Never ever do that.
    When using a pointer to point to a const string like above, make sure to use const.

    Always declare like this...
    Code:
    const char *const_str = "a test";
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  14. #14
    Join Date
    Jun 2002
    Posts
    137

    Re: Is this valid initialize of char []

    to microcode: it is for initialization of the array. maybe you can see more clearly as following example:
    Code:
    char* dent[] = {NULL, NULL};
    for approach:
    Code:
    char const_str[] = "a test";
    
    char* dent[] = {const_str};
    The problem is that const_str1, const_str2 are passed into the function as char* type arguments. currently in that function what I do is, though it is not good approach since I will have to init the dent far from its declaration,

    Code:
    char* dent[2] = {0};
    
    //many lines of other declarations between
    
    dent[0] = const_str1;
    dent[1] = const_str2;

    Thanks!
    sandodo
    Last edited by sandodo; January 19th, 2006 at 04:01 AM.

  15. #15
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Is this valid initialize of char []

    Quote Originally Posted by sandodo
    dent[0] = const_str1;
    dent[1] = const_str2;
    I don't think here you will face any issues if you are using const char arrays (declared as constants) to initialize dent elements. But take care that you cannot change string elements of dent after that.. for that capability - you would instead of above, use strcpy. Regards.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured