Pass struct to a function.

This is a discussion on Pass struct to a function. within the C++ Programming forums, part of the General Programming Boards category; Say I define the following structure globally: Code: struct stfirst { int num; char let; }Me; then I go ahead ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    Pass struct to a function.

    Say I define the following structure globally:
    Code:
    struct stfirst
    {
         int num;
         char let;
    }Me;
    then I go ahead and declare it in main as follows:
    Code:
    stfirst Me[3] = {
         {1,'a'},
         {2,'b'},
         {3,'c'}
    };
    Is there a way to pass this struct of type first to a function called:
    Code:
    void show(stfirst);
    where the show function will then go through the struct and display the options? I cant seem to make mine work. I dont know what names to put in place of where (ie the function definition,function call from main, and function body)


    so does this now mean that I am passing an array of type stfirst to the function, then I should do as if I were just passing an array to the function.. ie
    Code:
    void show(Me[3])
    {
    //do comands
    }
    ??
    and when I call the function I should use the array name which was Me.
    Last edited by Hybird; 11-26-2005 at 09:45 PM.

  2. #2
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You could do it that way. Or this way..

    Code:
    #include <iostream>
    
    struct stfirst
    {
         int num;
         char let;
    };
    
    void show(stfirst* me)
    {
         for(int i = 0; i < 3; i++) {
                 std::cout << me[i].num << std::endl;
                 std::cout << me[i].let << std::endl;
         }
         
    }
    
    int main()
    {
        stfirst me[3] = {
         {1,'a'},
         {2,'b'},
         {3,'c'}
         };
         
         show(me);
    
        std::cin.get();
    }
    That could be theoretically wrong (as in it works, but you shouldnt do it). I've been wrong too much when it comes to passing parameters, references, pointers, and how to treat them.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    But when I try my program it doesn work. I can pass a single struct to my function, but I cant pass the array of struct type stfirst. to pass an array you just put the array name in the function when using it. and in the function definition you just use the type and then name of array. But it wont work for me..

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    I think you are asking for something like this?
    Code:
    #include <iostream>
    
    using namespace std;
    
    
    struct struc_name
    {
    	int num;
    	char letter;
    };
    
    
    void funk(struc_name name_in_function[] );
    
    
    int main()
    {
    
    	struc_name test[3] = { {1,'a'}, {2,'b'}, {3,'c'} } ;
    
    	funk(test);
    
    	cin.get();
    
    
    	
    return 0;
    }
    
    void funk(struc_name name_in_function[] )
    {
    
    	for (int i = 0; i< 3; i++)
    	{
    		cout << name_in_function[i].num << ',' << name_in_function[i].letter << endl;
    	}
    
    }
    Last edited by Enahs; 11-26-2005 at 10:34 PM.

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Using [] and * is the same thing, but I guess [] is more descriptive.

    Works for me Hybird, but I wouldn't deep copy it.

    Code:
    #include <iostream>
    
    struct stfirst
    {
         int num;
         char let;
    };
    
    void show(stfirst me[3])
    {
         for(int i = 0; i < 3; i++) {
                 std::cout << me[i].num << std::endl;
                 std::cout << me[i].let << std::endl;
         }
         
    }
    
    int main()
    {
        stfirst me[3] = {
         {1,'a'},
         {2,'b'},
         {3,'c'}
         };
         
         show(me);
    
        std::cin.get();
    }
    Maybe its because you are missing a datatype? for the parameter in show(). Or mismatches because you have that global object Me, which isn't necessary.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    stfirst Me[3]...

    Is there a way to pass this struct of type first to a function called:

    void show(stfirst);
    No. 'stfirst' and 'stfirst[3]' are different types. The way parameter passing works is: when you call a function, the variable listed as the parameter of the function is first created, and then it is initialized with the value you send to the function. That is why the type of the argument you send to a function has to match the type of the function's parameter variable.

    I also think you are misapplying Hungarian notation. I think the way you are supposed to use Hungarian notation is like this:
    Code:
    struct First
    {
       ....
    }
    
    First stMyVarName;
    In my opinion, the way you are using it now is confusing rather than informative.
    Last edited by 7stud; 11-26-2005 at 11:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to pass a pointer of a struct to a function
    By drty2 in forum C Programming
    Replies: 12
    Last Post: 01-19-2009, 11:34 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Possible Loss of data
    By silicon in forum C Programming
    Replies: 3
    Last Post: 03-24-2004, 11:25 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

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