Thread: error: ISO C++ forbids assignment of arrays

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    error: ISO C++ forbids assignment of arrays

    The problem is when I'm trying to compare two strings. a1 and " ". The string a1 is set to [3].

    If the comparison is true then it makes a1 = ".."

    Here is the source code. I made most of it when I only read half of my C book, so PLEASE, bear with me. I know there is a lot of security flaws, and tricks to make this much more compact and clean. I'll do that latter.

    Right now, functionality is numero uno

    Important Source Code:
    Code:
        int maxC;
        int max1;
        int max2;
        int max3;
        int max4;
    
       char a1[3] = "  ";
       char a2[3] = "  ";
       char a3[3] = "  ";
    
    		for (; max1>0; )
    		{
                compare = strcmp(a1,"  ");
    			if (compare==0)
    			{
                       a1 = "..";
                       max1= max1 - 2;
                       continue;
    			}
    			
                compare = strcmp(a2,"  ");
    			if (compare==0)
    			{
                       a2 = "..";
                       max1= max1 - 2;
                       continue;
    			}
    			
                compare = strcmp(a3,"  ");
    			if (compare==0)
    			{
                       a3 = "..";
                       max1= max1 - 2;
                       continue;
    			}
          	}
    Full Source Code:
    LS.C

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The error says what it says: you are not allowed to assign to an array. What you can do is use strcpy, memcpy and the like to copy the content.

    Also, the error message mentions C++, which probably means that you are using a C++ compiler to compile your C program. While this is not necessarily wrong, it is probably not what you want to do.
    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
    Sep 2011
    Posts
    117
    Very interesting, I'm on page 321 of 358, and what do you know, strings are the last section.

    Just learned a string is just an array of characters, which explains the error code and my confusion. Used strcpy and works perfect. I am aware of the C++ compiler problem. Will fix it when I have the patience. Thanks a bunch!

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by JonathanS View Post
    Very interesting, I'm on page 321 of 358, and what do you know, strings are the last section.

    Just learned a string is just an array of characters, which explains the error code and my confusion. Used strcpy and works perfect. I am aware of the C++ compiler problem. Will fix it when I have the patience. Thanks a bunch!
    What compiler are you using? There usually is a simple solution for this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    117
    Bloodshed Dev-C++. I tried uninstalling it and putting in mingw but it just gave me a bunch of problems.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JonathanS View Post
    Bloodshed Dev-C++. I tried uninstalling it and putting in mingw but it just gave me a bunch of problems.
    DevC++ has been abandoned for at least 6 years. The compiler supplied with it is at least 7 years out of date...

    Why are you playing with Abandonware when there are several free and up-to-date compilers readily available on the net...

    I usually recommend Pelles C for C-99 ... no C++ crap to get in the way, 32 and 64 bit compilers and the best help file you could ask for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: ISO C++ forbids declaration of 'string' with no type
    By TheBigOnion in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2010, 06:33 AM
  2. assignment of arrays problem
    By HumbuckeR in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2006, 04:25 PM
  3. ISO C++ Forbids it!
    By Jimmey in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 05:40 PM
  4. Assignment of Two-Dimensional Arrays
    By LuckY in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2004, 03:40 PM
  5. Assignment of two char arrays
    By moemen ahmed in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2002, 12:44 AM