a struct in a function parameters

This is a discussion on a struct in a function parameters within the C++ Programming forums, part of the General Programming Boards category; ive started to learn c++ for halflife modding , and ive come across something which is puzzling me . void ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    306

    a struct in a function parameters

    ive started to learn c++ for halflife modding , and ive come across something which is puzzling me .

    void NET_CALLBACK ListResponse( struct net_response_s *response );

    now have can you have a function like this
    with 2 function names plus saying struct in the parameters..
    and ive seen struct within a class..all this is new to me
    and the programmers of this mod sdk was able to use
    CHudServers *g_pServers; /////////////// -> on its members without
    making CHudServers *g_pServers = new Chudservers...
    can someone clear some of this up :P

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Anddos
    ive started to learn c++ for halflife modding , and ive come across something which is puzzling me .

    void NET_CALLBACK ListResponse( struct net_response_s *response );

    now have can you have a function like this
    with 2 function names plus saying struct in the parameters..
    NET_CALLBACK is the calling convention, I havn't come accross this one in particular but it tells the compiler how to call the function. ListResponse is the name of the function.

    and ive seen struct within a class..all this is new to me
    and the programmers of this mod sdk was able to use
    CHudServers *g_pServers; /////////////// -> on its members without
    making CHudServers *g_pServers = new Chudservers...
    can someone clear some of this up :P
    If your function is passed a pointer to a structure then it should have allready been created somewhere else so you don't need to call new to create another structure. the -> operator dereferences the pointer allowing you to acces it's members.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    306
    well its done #define NET_CALLBACK

    then used the NET_CALLBACK for the function names
    is it sort of grouping the functions to a catagory

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    now have can you have a function like this
    with 2 function names plus saying struct in the parameters..
    translation: How can you use the keyword "struct" in a function parameter?
    Code:
    #include <iostream> 
    using namespace std;
    
    struct Data
    {
    	int num1;
    	int num2;
    };
    
    void show(struct Data* ptr)
    {
    	cout<<"Data:\n"
    	    <<ptr->num1<<endl
    	    <<ptr->num2<<endl;
    }
    int main()
    {
    	struct Data myData;
    	myData.num1 = 20;
    	myData.num2 = 30;
    
    	show(&myData);
    	
    	return 0;
    }
    The code:
    Code:
    ptr->num1
    is shorthand for:
    Code:
    (*ptr).num1
    Last edited by 7stud; 02-14-2006 at 11:09 AM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    In C++ the struct keyword is optional everywhere except the original definition.
    Code:
    #include <iostream> 
    using namespace std;
    
    struct Data
    {
    	int num1;
    	int num2;
    };
    
    void show(struct Data* ptr)
    {
    	cout<<"Data:\n"
    	    <<ptr->num1<<endl
    	    <<ptr->num2<<endl;
    }
    int main()
    {
    	struct Data myData;
    	myData.num1 = 20;
    	myData.num2 = 30;
    
    	show(&myData);
    	
    	return 0;
    }
    blue = optional
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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