By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,948 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,948 IT Pros & Developers. It's quick & easy.

Problem Initialising char arrays

P: n/a
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 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

sh*******@yahoo.com 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-----
Nov 15 '05 #2

P: n/a
On 13 Nov 2005 22:45:49 -0800, in comp.lang.c , sh*******@yahoo.com
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 =----
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.