Thread: Array copying problem

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 09:19 AM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  4. Problem Putting INTs Into a CHAR Array
    By cram in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2004, 07:53 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM