passing struct as reference

This is a discussion on passing struct as reference within the C Programming forums, part of the General Programming Boards category; Hey im trying to pass a struct by reference to change one of its variables inside the void function. There ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    passing struct as reference

    Hey im trying to pass a struct by reference to change one of its variables inside the void function. There is my code:

    Code:
    typedef struct info_{
    	MYSQL*	p_Conn;
    	char* 	Host;
    	char* 	User;
    	char* 	Password;
    	char* 	Database;
    }info;
    
    int main(){
            ....
            info 	ConnInfo;
            Initiate_mysql(&ConnInfo);
    }
    
    void Initiate_mysql(info *ConnInfo){
    	(*ConnInfo).p_Conn = mysql_init(NULL);
    	...	
    }
    im getting an error in execution because of ConnInfo declaration... do i have to allocate something?
    Last edited by ltcabral; 03-13-2008 at 10:20 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    You use p_Connection but declared p_Conn. Also, are you sure that mysql_init() returns a MYSQL object instead of a MYSQL pointer?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    My bad... already corrected these mistakes... but thats not the main problem... i wanna know why i cant even start the struct,

    mysql_init(): Allocates or initializes a MYSQL object suitable for mysql_real_connect(). If mysql is a NULL pointer, the function allocates, initializes, and returns a new object. Otherwise, the object is initialized and the address of the object is returned.
    Last edited by ltcabral; 03-13-2008 at 10:29 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    i wanna know why i cant even initialize the struct
    Without referring to the MySQL API docs, your code looks okay. The problem probably lies in those dots. You should post the smallest and simplest compilable program that demonstrates the error, and state what exactly is the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    You realize it's possible to do
    ConnInfo->p_Conn
    instead of
    (*ConnInfo).p_Conn
    Right?

    And I find it better to say "pass by pointer" rather than "by reference," since IMO, reference is limited only to C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    This was my last code, using a double pointer as parameter, thats exactly what i want my structure to do:

    Code:
    int main(){        
    
    	MYSQL	*p_Connection;
            ...
            Initiate_mysql(&p_Connection);
            ...
    }
    
    void Initiate_mysql(MYSQL** p_Connection){
    	const char*	Host = "xxxx";
    	const char*	User = "xxxx";
    	const char*	Password = "xxxx";
    	const char*	Database = "xxxx";
    	
    	*p_Connection = mysql_init(NULL);
    	
    	...
    	
    }
    can u tell if my first code is doing the same as this one? im not sure if the
    (*ConnInfo).p_Conn = mysql_init(NULL);
    is doing exactly what i want since i cant execute to test it...

    You realize it's possible to do
    ConnInfo->p_Conn
    instead of
    (*ConnInfo).p_Conn
    Right?
    thx ill change that also

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,556
    The first code was also right...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    can u tell if my first code is doing the same as this one?
    No, since only you know what's with the dots....

    However, I can say that this:
    Code:
    int main() {
        MYSQL *p_Connection;
        Initiate_mysql(&p_Connection);
    }
    
    void Initiate_mysql(MYSQL** p_Connection) {
        *p_Connection = mysql_init(NULL);
    }
    is functionally equivalent to:
    Code:
    typedef struct info_ {
        MYSQL *p_Connection;
    } info;
    
    int main() {
        info ConnInfo;
        Initiate_mysql(&ConnInfo);
    }
    
    void Initiate_mysql(info *ConnInfo) {
        ConnInfo->p_Connection = mysql_init(NULL);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  2. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

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