Thread: copying the arrays

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    33

    copying the arrays

    i want to fill the static values of the array into another array,its working fine,just want to know ,is this is right or any other method for copying the arrays..



    Code:
    #include<stdio.h>
    #define SIZE 1024
    int main()
    {
        int array[SIZE];
        int array1[] = { 10,20,7,8,4,5,3,2,21,6,7,5,3,2,2,2,4,5,67,8,9,6,64,5,4,3,2,2,2,2,2};
        int i;
        printf("\n The Array values are \n");
        
         copy_array(&array,&array1);
        for(i = 0;i<SIZE;i++)
        
            printf("%d  ",array[i]);
        
        for(i = 0;i<32;i++)
        
            printf("\n\n\n%d  ",array1);
    
        
    
    
        
    }
     copy_array(int *array,int *array1)
    {
        int i;
        for(i = 0;i<32;i++)
        {
            array[i]= array1;
        }    
        for(i = 0;i<1024;i++)
        
            printf("%d  ",array[i]);
        
        for(i = 0;i<32;i++)
        
            printf("\n\n\n%d  ",array1);
    
        
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Generally speaking, copy no more than needed, and ensure you don't overrun either of the arrays. Overrunning an array you're copying from is just as bad an idea as overrunning an array you are copying to.

    There are plenty of methods. Look up the memcpy() function, for one.

    Also, the word "static" does not mean what you think it does. Your code has no static arrays.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    i need to write the function to copy the values of the array from one to another

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Are you sure it works?
    Code:
    $ gcc -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:10:6: warning: implicit declaration of function ‘copy_array’ [-Wimplicit-function-declaration]
    foo.c:17:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
    foo.c: At top level:
    foo.c:24:2: warning: return type defaults to ‘int’ [-Wreturn-type]
    foo.c: In function ‘copy_array’:
    foo.c:29:17: warning: assignment makes integer from pointer without a cast [enabled by default]
    foo.c:37:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
    foo.c:41:1: warning: control reaches end of non-void function [-Wreturn-type]
    foo.c: In function ‘main’:
    foo.c:23:1: warning: control reaches end of non-void function [-Wreturn-type]
    Things like this
    array[i]= array1;
    are not copying the array.
    It's just making every element 'point' to the start of another array. Except it's a pointer coerced into an integer.

    You need
    array[i]= array1[i];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    33
    sorry its array[i]

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > sorry its array[i]
    Where?

    Why do I get the feeling all of a sudden that the code you posted is not your real code, but something you half remember because you're not "at your computer" at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying arrays...
    By darren78 in forum C++ Programming
    Replies: 7
    Last Post: 08-20-2010, 10:55 AM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. needs help copying arrays
    By RedRattler in forum C Programming
    Replies: 3
    Last Post: 04-04-2003, 11:34 PM
  4. Copying Arrays
    By conright in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 04:09 PM
  5. help on copying arrays
    By neversell in forum C Programming
    Replies: 3
    Last Post: 06-30-2002, 04:22 PM

Tags for this Thread