Thread: Assign an arrays values to another array

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    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.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You cannot assign to arrays, so you should use say, strcpy() instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #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]).

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    19

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-10-2007, 05:54 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Eliminating duplicate values from a 20x2 array
    By desipunjabi in forum C Programming
    Replies: 2
    Last Post: 10-29-2005, 09:11 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Inputted values into an array
    By Panther in forum C Programming
    Replies: 6
    Last Post: 04-11-2003, 10:15 AM

Tags for this Thread