1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Array copying problem

Discussion in 'C' started by arun10427, Mar 4, 2010.

  1. arun10427

    arun10427 New Member

    Joined:
    Mar 4, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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. ungalnanban

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    Last edited: Mar 4, 2010
  3. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    You can not straight away copy the structure ,you need to pass the address of the structure to the function then using that address you can copy the contents.
     
  4. arun10427

    arun10427 New Member

    Joined:
    Mar 4, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    @ungalnanban

    Thanks for ur reply..
    But when i call the function in main, I get this error.
     
  5. arun10427

    arun10427 New Member

    Joined:
    Mar 4, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    This is the error message:


    error: incompatible type for argument 1 of ‘copy_struct’
    note: expected ‘struct instruction *’ but argument is of type ‘instruction’
    error: incompatible type for argument 2 of ‘copy_struct’
    note: expected ‘struct instruction *’ but argument is of type ‘instruction’

    Am I making some mistake in calling the fuction?
     
  6. abubacker1

    abubacker1 New Member

    Joined:
    Feb 20, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    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);
    }
    
     
  7. abubacker1

    abubacker1 New Member

    Joined:
    Feb 20, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    The above code is similar to arun10427 code , but I changed the function call as a
    call by reference.
     
  8. abubacker1

    abubacker1 New Member

    Joined:
    Feb 20, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    want to copy the whole struct
    try this
    *dest =*src ;
     
  9. ungalnanban

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    See the following Example:


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

Share This Page