Problem Initialising char arrays

S

shan_rish

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
 
L

Lew Pitcher

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

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-----
 
M

Mark McIntyre

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top