C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-06-2009, 05:54 AM   #1
Registered User
 
Join Date: Dec 2008
Posts: 13
Question [SOLVED] Assign an arrays values to another array

Hello World!

I have a file stream called "src" and I am reading in lines from that file. The file contains information about sms messages. The very first line tells us, how many messages are stored in the file. Then, every message takes up two lines: the first line containing information about the date of reception and the sender, the second line containing the message itself, with a length of 100 chars at maximum. The file contains 30 messages, thus it is 60+1 lines long. My task is, to tell which message is the shortest, and which one is the longest. I have started with this code:

Code:
...includes...
...var declarations...

char shortest_msg[100];
char longest_msg[100];

...some code...
...even more code...

for(i=0;i<=number_of_msg*2;i++){
		fgets(current_line,100,src);
		if(i==0)
			continue;
		else if(i/2==0){
		}
		else if(i/2==1){
			if(strlen(shortest_msg)>strlen(current_line))
				shortest_msg=current_line;
			else if(strlen(longest_msg)<strlen(current_line))
				shortest_msg=current_line;
		}
	}
I am trying to assign a character array returned by fgets() to another character array. Both of them are of the same length, 100 chars. The compiler keeps telling me, that there is an "invalid lvalue assignment". What am I doing wrong?

Last edited by laczfinador; 05-06-2009 at 07:46 AM.
laczfinador is offline   Reply With Quote
Old 05-06-2009, 06:19 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
You cannot assign to arrays, so you should use say, strcpy() instead.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 05-06-2009, 06:23 AM   #3
Registered User
 
Join Date: May 2009
Posts: 4
use memcpy() to copying current_line to shortest_msg, I think you unable to assign another address of pointer ( shortest_msg is address of shortest_msg[0] same as &shortest_msg[0]).
ReeV is offline   Reply With Quote
Old 05-06-2009, 07:46 AM   #4
Registered User
 
Join Date: Dec 2008
Posts: 13
Thumbs up

Greetings, and thanks fro the replies!

The strcpy() function did the job well, and it seems easy to use even. Wish everything couls be solved so easily...
laczfinador is offline   Reply With Quote
Reply

Tags
array, arrays, fgets, invalid lvalue assignment

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Static Arrays, C++ uses an odd method of assigning values? Norehsa C++ Programming 5 06-10-2007 05:54 AM
question about multidimensional arrays richdb C Programming 22 02-26-2006 09:51 AM
Eliminating duplicate values from a 20x2 array desipunjabi C Programming 2 10-29-2005 09:11 PM
Type and nontype parameters w/overloading Mr_LJ C++ Programming 3 01-02-2004 01:01 AM
Inputted values into an array Panther C Programming 6 04-11-2003 10:15 AM


All times are GMT -6. The time now is 10:58 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22