Thread: Array Problem

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Array Problem

    What is wrong with the following code segment?

    Code:
    const int limit = 100;
    int eprep[limit];
    int examp[limit];
    
    for (int index = 0; index <= limit - 1; index++)
    {
    	eprep[index] = 0;
    	examp[index] = 0;
    }
    if (eprep == examp)
    cout << "Equal";
    At first I thought it was maybe the fact that it has no 'else' but then thought that was too easy of an answer...????

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Depends on what you mean by "wrong". eprep and examp are not the actual same array in memory, which is what you are checking. eprep and examp may have all the elements the same, but that doesn't mean they are in the same physical place.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    48
    eprep and examp are pointers to different memory addresses and obviously are not equal.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    An easy way to test for equality in two arrays is the equal templated function in the <algorithm> header:
    Code:
    #include <algorithm>
    
    ...
    
    if( std::equal(eprep,eprep+limit,examp) )
      std::cout << "Equal";
    I think that's how you use it... it's been awhile.


    Code:
    for (int index = 0; index <= limit - 1; index++)
    That's the same as (and usually written as):
    Code:
    for (int index = 0; index < limit; index++)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array copy problem
    By Witch in forum C Programming
    Replies: 3
    Last Post: 03-22-2010, 07:00 PM
  2. Sorting array problem :)
    By BEST in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2009, 01:57 PM
  3. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  4. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM