473,473 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem Initialising char arrays

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
2 4671
-----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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
5
by: cpptutor2000 | last post by:
I am compiling and running the following code snippet on a Linux box - I am really puzzled by the answers. Could someone please tell me what might be wrong? void test(){ int m = 0; int n = 0;...
1
by: rir3760 | last post by:
Since a few days ago I have been working with the program I post below (a school assignment). The purpose of the program is to work with the va_ macros (stdarg.h) and arrays of arrays, hopefully...
46
by: Albert | last post by:
Why doesn't: #include <stdio.h> void reverse(char, int); main() { char s;
6
by: Jeremy Targett | last post by:
Hello, I'm trying to initialise a two-dimensional array of characters, 12 by 60, with each entry maximum 6 characters wide. (It's a fix I'm making to an old program which I learned just enough C...
5
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sþ0þ%6s Heres the data: SS000000395000000000DC-þ0þ799829þ1174503725þ Actually, I...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
22
by: Wilson | last post by:
i am learning to program using c++ and was set a task of making a simple encryption algorithim. I choose to start with one where simply each letter is replaced with its equivilent in the alphabet...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.