Thread: function that takes struct?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    function that takes struct?

    how do u put a struct from main to a function so that the function
    would display it?

  2. #2
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    Code:
    #include <stdio.h>
    #include <iostream.h>
    #include<string.h>
    
    struct myStruct{
    int m;
    int c;
    };
    void myFunc(myStruct &);    //prototype for function
    
    int main(int argc, char *argv[])
    {
    myStruct s1;
    myFunc(s1);                 //pass by refrence
    cout<<"S1.m= "<<s1.m<<endl;
    cout<<"S1.c= "<<s1.c<<endl;
    cin>>"press enter";
    
      return 0;
    }
    
    void myFunc(myStruct & refStruct){
         refStruct.m=5;
         refStruct.c=10;
         return;
         }
    hope i helped you
    Programming is a high logical enjoyable art for both programer and user !!

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    thanx,


    what's refstruct?

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    but i cant use this for any structure

  5. #5
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    refStruct ----> is a reference (check up using references in any book or tutorial).........

    I have to pass the structure as reference not by value from main to the fuction as passing by value will occure error coz the structure ll be out of scope (as the structure ll be local in the main)
    so passing by reference (using the operator &) ll solve this problem .


    sure you can use this way for any structure (give me exact problem , I ll show you how to solve it )

    good luck
    Programming is a high logical enjoyable art for both programer and user !!

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    well try this

    myfunc(take any struct)
    {
    //do stuff
    }

    what u did was something like

    myfunc(existing struct )


    do u get my point?

  7. #7
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    execuse me.........i cant get your point , u may clear urslef more , please
    Programming is a high logical enjoyable art for both programer and user !!

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    pode, you are misunderstanding the situation.

    myFunc(myStruct &), means that 'myFunc' accepts a reference
    to a structure. It can be any structure.

    I think that you should read up about function parameters and
    arguments.

  9. #9
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    this is a complete chapter from "teach yourself c++"
    about references, all u need about this topic ll be answered in this chapter.its very clear and full of examples, read it carefully, im ready to help u if u missunderstand anything within .......


    alright??? good luck
    Programming is a high logical enjoyable art for both programer and user !!

  10. #10
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Posts
    107
    Do you mean that you want to print contents of any struct? Then you need to use templates and provide a method that prints the contents in the struct so that the function can call it.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by pode
    but i cant use this for any structure
    No, you can't. Not for ANY structure at least.

    You have to know what type of structure you are dealing with to be able to use it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    If you had declared an array of structures you wouldn't of needed to pass it by reference, as the address is automatically passed when dealing with arrays. Is this right?
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 06:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM