CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: passing structure in a function

  1. #1
    Join Date
    Mar 2005
    Posts
    33

    passing structure in a function

    hi,

    I have a confusion. I am passing a structure to a function, where values are filled in the structure. I am passing it using a pointer and if i want to have a copy of that structure in another function how can i do that?

    typedef struct
    {
    int age;
    int name;
    }emp;

    void main()
    {
    emp obj={20,"apple"};

    fun_print(&emp);

    }

    void fun_print(struct *emp)
    {
    struct emp *obj2;
    obj2= new emp; //allocate mem
    obj2=emp; // will this be another copy of the struct ?? or just another pointer pointing to that struct??


    }

    thanks

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: passing structure in a function

    You may be getting confused between objects and types.

    You have created a type "emp", and allocated two objects of this type: "obj" in main() which is on the stack, and an emp on the heap in fun_print() which is pointed to by obj2.

    You have not properly passed obj (or even a pointer to obj) into fun_print, nor have you correctly specified the function's parameter. There is also no obvious reason why the second emp needs to be on the heap.

    As far as copying goes, just use the copy constructor or use assignment. Note, however, that since obj2 is a pointer, assigning to obj2 will only modify the pointer. If you wish to modify what it points to you need to dereference obj2 in the assignment.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,588

    Re: passing structure in a function

    Quote Originally Posted by varsha5in View Post
    hi,

    I have a confusion. I am passing a structure to a function, where values are filled in the structure. I am passing it using a pointer and if i want to have a copy of that structure in another function how can i do that?

    typedef struct
    {
    int age;
    int name;
    }emp;

    void main()
    {
    emp obj={20,"apple"};

    fun_print(&emp);

    }

    void fun_print(struct *emp)
    {
    struct emp *obj2;
    obj2= new emp; //allocate mem
    obj2=emp; // will this be another copy of the struct ?? or just another pointer pointing to that struct??


    }

    thanks
    A better way would be to pass by reference. Your function should look like
    Code:
    void fun_print(emp& Employee)
    Then call it like
    Code:
          fun_print(obj);
    No need for new or pointers in your function at all.

    Pay attention too to what Lindley said. You're confusing some basic concepts.

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: passing structure in a function

    1. main() never returns void, it is always int.
    2. typedef struct isn't necessary in C++
    3. pass a struct or class by reference, not by pointer
    4. I wouldn't be so casual about abbreviating things, it makes you code impossible to understand at first glance.
    5. Using code tags will also make it easier to read

    Code:
    struct Employee
    {
        int age, name;
    };
    
    int main()
    {
        Employee emp1 = { 20, "Mike" };
        print_employee(emp1);
    }
    
    void print_employee(const Employee& emp)
    {
        Employee emp2 = emp; // emp2 is now a copy of emp, which is a reference to emp1
    }
    You might want to look for other learning materials. I have a feeling you are actually reading about C, although you did manage to throw a "new" operator in there, so maybe you are reading poor C++ material.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)