Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    problem with a string C/C++ =[

    I think this is going to sound really stupid but:

    Code:
    char buffer[256];
    buffer = "example of a line\n";
    I'm getting a: "incompatible types in assignment":
    a declaration like: char * buffer is giving me the same problem.

    i must be missing something really obvious here.
    Last edited by klam; 09-17-2007 at 08:30 PM.

  • #2
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    "example of a line\n" is a const char*, so you can't assign it to a char* without either (a) doing it at the same time you declare the (const char*) buffer or (b) adding an intermediate step, most commonly strcpy(char* dest, const char* source). Casting also works for (b) but strcpy is more correct.

    In C++ you almost certainly want to use std::string instead and duck the whole issue.
    Last edited by ralph l mayo; 09-17-2007 at 09:46 PM.

  • Users who have thanked ralph l mayo for this post:

    klam (09-17-2007)

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey Ralph, yeah a const, what was i thinking!...

    Thanks


  •  

    Posting Permissions

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