Thread: question relating to arrays

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    53

    Unhappy question relating to arrays

    Hey guys, i've had sooo much trouble with just something that is probably really simple, would appreciate the answer please

    I'm trying to make one array equal to another array, to make it worse i'm trying to make an array of a struct equeal to another one of the same type but I dont think that is the issue I think its just getting one array to equal another, below is sort of a list i made of things ive tried that have not worked

    Code:
    int arrayOne[10] = {6, 5, 7, 3, 2, 5, 6, 5, 4, 5};
    int arrayTwo[10];
    
    
    arrayTwo = arrayOne;
    arrayTwo[10] = arrayOne[10];   //I'm guessing this will just make the 10th element equal to //arrayOne's 10th element, which is probably stupid since its the null terminator???
    should I just make a for loop to do it or is there an easier way????

    Thanks for the help.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I'm not sure if I get what you are saying correctly but you should use '==' for comparison, and '=' for assignment. You could use a loop to iterate through the array and determine if they are equal or you could use memcmp, like this.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
            int array1[3] = {1,2,3};
            int array2[3] = {1,2,3};
    
            if(memcmp(array1, array2, sizeof(array1)) == 0)
                    puts("equal");
            else
                    puts("unequal");
    
    
            return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    As far as I know, you're going to need to loop through the arrays and set each element of arrayTwo to its corresponding element in arrayOne. A for loop would do best, I think.
    I might not be a pro, but I'm usually right

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Are you trying to copy one array to the other? If so you could use memcpy, assuming the arrays are of the same type and size of course.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about arrays
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 11-08-2006, 03:09 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 08:24 PM
  3. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 02:57 AM
  4. Question about char arrays
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 12:44 AM
  5. Question about arrays?? PLease read..
    By foofoo in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 02:40 PM