Problem Initialising char arrays

Discussion in 'C Programming' started by shan_rish@yahoo.com, Nov 14, 2005.

  1. Guest

    Hi CLCers,
    In the below program the error message while compiling is

    /home1/murugan/prog/cprog >cc -o struct_eval struct_eval.c
    cc: "struct_eval.c", line 14: error 1549: Modifiable lvalue required
    for assignment operator.
    cc: "struct_eval.c", line 17: error 1549: Modifiable lvalue required
    for assignment operator.
    cc: "struct_eval.c", line 18: error 1549: Modifiable lvalue required
    for assignment operator.
    /home1/murugan/prog/cprog >


    In line 13 iam initilaising the char array test2 with "Test\n"
    successfully, but for other char arrays the above error is thrown. Any
    help in understanding the problem is appreciated. The program is

    #include<stdio.h>


    int main()
    {
    struct data
    {
    char char1[100];
    char char2[100];
    };

    char test1[100];
    char test2[100]="Test\n";
    test1 = "Test\n";

    struct data data1;
    data1.char1="Char 1 Data1\n";
    data1.char2="Char 2 Data1\n";
    printf("Plain Struct1 = %s \n Plain Struct1 = %s \n", data1.char1,
    data1.char2);
    return 0;
    }


    Thanks in advance.

    Cheers
    Shan
     
    , Nov 14, 2005
    #1
    1. Advertising

  2. Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    wrote:
    > Hi CLCers,
    > In the below program the error message while compiling is
    >
    > /home1/murugan/prog/cprog >cc -o struct_eval struct_eval.c
    > cc: "struct_eval.c", line 14: error 1549: Modifiable lvalue required
    > for assignment operator.
    > cc: "struct_eval.c", line 17: error 1549: Modifiable lvalue required
    > for assignment operator.
    > cc: "struct_eval.c", line 18: error 1549: Modifiable lvalue required
    > for assignment operator.
    > /home1/murugan/prog/cprog >
    >
    >
    > In line 13 iam initilaising the char array test2 with "Test\n"
    > successfully, but for other char arrays the above error is thrown. Any
    > help in understanding the problem is appreciated. The program is
    >
    > #include<stdio.h>
    >
    >
    > int main()
    > {
    > struct data
    > {
    > char char1[100];
    > char char2[100];
    > };
    >
    > char test1[100];
    > char test2[100]="Test\n";
    > test1 = "Test\n";


    test1 is an array of char. With this assignment, you are trying to initialize
    test1 with the address of a fixed string. You can't initialize a character
    array in this manner.

    Your choices are:

    a) leave test1 defined as a char array, and
    strcpy(test1,"Test\n");

    b) change test1 to be a pointer to char
    char *test1;
    and initialize the pointer to point to the fixed string
    test1 = "Test\n";

    c) change test1 to be a pointer to char, and initialize it to point to a fixed
    string
    char *test1 = "Test\n";

    d) leave test1 defined as a char array, and initialize it to a fixed string
    char test1[100] = "Test\n";


    > struct data data1;
    > data1.char1="Char 1 Data1\n";


    data1.char1 is an array of char. You are making the same mistake as with
    test1. You fix it the same way


    > data1.char2="Char 2 Data1\n";


    data1.char2 is an array of char. You are making the same mistake as with
    test1. You fix it the same way

    > printf("Plain Struct1 = %s \n Plain Struct1 = %s \n", data1.char1,
    > data1.char2);
    > return 0;
    > }


    - --
    Lew Pitcher

    Master Codewright & JOAT-in-training | GPG public key available on request
    Registered Linux User #112576 (http://counter.li.org/)
    Slackware - Because I know what I'm doing.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)

    iD8DBQFDeDeLagVFX4UWr64RAiIGAKC6WoRGlnV7wqpXEMyw/i98OAR1nwCg3G/y
    eXeI/YGowOkwpiyy82R4wlo=
    =wUL7
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Nov 14, 2005
    #2
    1. Advertising

  3. On 13 Nov 2005 22:45:49 -0800, in comp.lang.c ,
    wrote:

    > char test1[100];
    > char test2[100]="Test\n";
    > test1 = "Test\n";


    You can't do that.

    Its slightly confusing, but when DECLARING an array, you can also
    initialise it with a string.

    However if you've already declared the array, you can't later on
    assign a string to it. So test2 is ok, test1 isn't.



    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Mark McIntyre, Nov 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Reyn
    Replies:
    3
    Views:
    2,341
    Bob Hairgrove
    Jun 22, 2004
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,151
    Ian Collins
    May 9, 2006
  3. Replies:
    3
    Views:
    760
  4. Christoph Conrad
    Replies:
    2
    Views:
    327
    Christoph Conrad
    Mar 1, 2007
  5. Philipp
    Replies:
    21
    Views:
    1,186
    Philipp
    Jan 20, 2009
Loading...

Share This Page