Array Problem

This is a discussion on Array Problem within the C++ Programming forums, part of the General Programming Boards category; What is wrong with the following code segment? Code: const int limit = 100; int eprep[limit]; int examp[limit]; for (int ...

  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,185
    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,806
    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, 08: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, 12:35 PM
  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, 03:28 PM

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