Thread: array problem

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    array problem

    I found it useless to do something on array such as if(myArray=[1,2,3]), and the only way I figure out is to compare the elements in each array separately just like this:

    for(i=0; i<3; i++)
    {
    if(myArray[i]==myVar)
    {
    doThis();
    }
    }


    is there any other way conveniently ?
    Never end on learning~

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    I dont think so

    Unfortunately, I dont think you can do that for integer arrays.

    However, you can compare char arrays using

    strcmp(array1, array2)

    This is how I think you can compare integer arrays
    PHP Code:
    int flag 0;
    int myArray = {1,2,3}
    int myVar = {1,2,3};

    for(
    i=0i<3i++) 

          if(
    myArray[i]==myVar[i]) 
          {
                  
    flag++;
          }

    so if flag = 3, it means all elements are the same.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    sorry, forgot to mention myVar was a variable itself.

    and I just want figure out how could we do something like this:
    if(myArray==[1,2,3]).

    thanx in advance~
    Never end on learning~

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    not possible

    to the best of my knowledge, i dont think you can do that directly.

    why dont you try making a function yourself... just like strcmp
    call it

    intarraycmp(int *array1, int *array2)

    it should return 0 if there are no differences, and 1 if there's
    atleast one difference.

    Use the same logic as I did in my previous post.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Moonwalker is correct because 'myArray' acts as a pointer to the first location (zero-position) of the array.

    Without incrementing the pointer, as with the FOR loop that was shown, it will simply sit at the first array position. Good for comparing that value, but not very handy for comparing the remainder of the array.

    The function idea is a good one.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    thanx guys~ I'll do better with array.
    Never end on learning~

  7. #7
    Basementman
    Guest

    Post array comparison

    If you have two array pointers to int arrays, and both arrays have a fixed size which is known, why not just do this:

    BOOL CompareIntArrays(int *ipArray1, int *ipArray2, int iArraySize)
    {
    return (memcmp(ipArray1, ipArray2, iArraySize * sizeof(int)) == 0);
    )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  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