Array copying problem

This is a discussion on Array copying problem within the C Programming forums, part of the General Programming Boards category; Hi I am trying to copy one struct to another, but I am making some minor mistake that it is ...

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

    Array copying problem

    Hi I am trying to copy one struct to another, but I am making some minor mistake that it is not getting copied. Can someone help me fix it?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct
    {
        int inst_addr;
    }instruction;
    
    void copy_struct(instruction src,instruction dest)
    {
        dest.inst_addr = src.inst_addr;
    }
    
    int main()
    {
        int addr = 1000;
        instruction check1;
        instruction check2;
        check1.inst_addr = 10;
        check2.inst_addr = 50;   
        copy_struct(check1,check2);
        printf("%d",check2.inst_addr);
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    36
    You just change the arguments to copy_struct function as follows.

    Code:
    void copy_struct(instruction *src,instruction *dest)
    {
        *dest=*src;
    }
    You also do one more change in calling that function as follows.

    Code:
    copy_struct(&check1,&check2);
    Last edited by vivekraj; 03-04-2010 at 01:12 AM.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    Pass the address of the structure.Then inside the function access the member using '->'.Because it is a pointer variable.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct
    {
    int inst_addr;
    }instruction;
    
    void copy_struct(instruction *src,instruction *dest)
    {
        dest->inst_addr = src->inst_addr;
    }
    
    int main()
    {
        int addr = 1000;
        instruction check1;
        instruction check2;
        check1.inst_addr = 10;
        check2.inst_addr = 50;
        copy_struct(&check1,&check2);
        printf("%d",check2.inst_addr);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by arun10427
    Hi I am trying to copy one struct to another, but I am making some minor mistake that it is not getting copied. Can someone help me fix it?
    As vivekraj suggested, you should change the function to have pointer parameters. However, instead of just assigning pointers, you should assign what the pointers point to, i.e.,
    Code:
    *dest = *src;
    EDIT:
    That said, this would be rather pointless since you can use it directly. A function like this will come in handy when you need to do a deep copy, e.g., you have a pointer member and you wish to copy what the pointer points to.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Actually we have answered here

    Array copying problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solve this problem using array..
    By juncas17 in forum C Programming
    Replies: 4
    Last Post: 10-12-2009, 10:19 AM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  3. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  4. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 08:53 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 07:24 PM

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