CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2012
    Location
    INDIA
    Posts
    25

    Unhappy character array please Help!!!

    void main()
    {

    char name[20];

    name = "omshanti";

    cout << name;

    }


    In the above program , if we won't initialize name as the type of its definition , why there's compile time error :
    cannot convert from 'char [9]' to 'char [20]'.???????

    while using other data type such as int , float , double , we can initialize them after defining them .But same is not the case with character string. WHY???????
    TANUSHREE-AGRAWAL...

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: character array please Help!!!

    But same is not the case with character string.
    Because a string is an array of characters. Use memcpy to put characters in a character array.

    Edit : That needs to be strcpy.
    Last edited by Skizmo; May 23rd, 2012 at 10:00 AM.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: character array please Help!!!

    You can't assign a string literal to a char array like that. Use strcpy.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: character array please Help!!!

    Quote Originally Posted by Skizmo View Post
    Because a string is an array of characters. Use memcpy to put characters in a character array.
    Why memcpy instead of strcpy?

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: character array please Help!!!

    Quote Originally Posted by GCDEF View Post
    Why memcpy instead of strcpy?
    Doh... sorry. Not having my day today. Of course it has to be strcpy.

  6. #6
    Join Date
    Apr 2012
    Posts
    43

    Re: character array please Help!!!

    Code:
    strcpy(name,"omshanti");

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: character array please Help!!!

    Quote Originally Posted by Tanushreeagr View Post
    Code:
    void main()
    {
    	...
    }
    You are using incorrect signature for main(): it must be int instead of void and it must return an integer value.
    Victor Nijegorodov

  8. #8
    Join Date
    May 2007
    Posts
    811

    Re: character array please Help!!!

    Why not std::string?
    Code:
    #include <string>
    // ...
    std::string name = "omshanti";
    or
    Code:
    #include <string>
    // ...
    std::string name;
    name = "omshanti";

  9. #9
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: character array please Help!!!

    Quote Originally Posted by Tanushreeagr View Post
    while using other data type such as int , float , double , we can initialize them after defining them .But same is not the case with character string. WHY???????
    You can't do that with other built-in types either.
    Code:
    int main()
    {
        int a[3];
        a = { 1, 2, 3 }; // error
    }
    You cannot assign to an array.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: character array please Help!!!

    Also, in the case of char name[20] both name and "omshanti" are interpreted by the compiler as pointers. name is essentially the same thing as &name[0].

    So you have a pointer to a locally allocated array of chars and you want to assign it the address of a string literal. Doesn't really make conceptual sense.

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