Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using structires 1

Status
Not open for further replies.

labnotes

Technical User
Sep 1, 2003
33
CA
I am working on a small program C++. I have information obtained in one function that will be used by many other functions. I am considering using a structure to store the information but I don't know how to share it between functions.

here is an example: first the header file

// public_dial.h

#define MAXINPUT 128 // Maximum string length everywhere
#define RESPLEN 2000 // Maximum response length


int start(char *host_name, char *user_name, char *password, char *db_name, unsigned int port_num, char *socket_name, unsigned int flags);

typedef struct //_tagCCommand
{
public:
char m_szhost_name[MAXINPUT];
char m_szuser_name[MAXINPUT];
char m_zpassword[MAXINPUT];
char m_zdb_name[MAXINPUT];
unsigned int m_nport_num;
char m_zsocket_name[MAXINPUT];
unsigned int flags;
} CCommand;

here is the .cpp

//-----------------------------------------------------------------------//
// P_Dial_port_test.cpp //

#include <stdio.h>
#include <stdlib.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <mysql.h>
#include "mysql_common.h"
#include "public_dial.h"
#include "getopt.h"

using namespace std;

#define def_host_name NULL // host to connect to (default = localhost)
#define def_user_name NULL // User name (default = your login name)
#define def_password NULL // password (default = none)
#define def_db_name NULL // database to use (default = none)
#define def_port_num 0 // use default port
#define def_socket_name NULL // use default socket
#define def_loc NULL // use default city
#define def_speed NULL // use default speed
#define def_tel_num NULL // use default tel number
#define def_ckt_num NULL // use default ckt number



char *groups[] = {"client", NULL};

struct option long_options[] =
{
{"host", required_argument, NULL, 'h'},
{"user", required_argument, NULL, 'u'},
{"password", required_argument, NULL, 'p'},
{"port", required_argument, NULL, 'P'},
{"socket", required_argument, NULL, 'S'},
{"location", required_argument, NULL, 'l'},
{"speed", required_argument, NULL, 's'},
{"tel", required_argument, NULL, 't'},
{"ckt", required_argument, NULL, 'c'},
{0,0,0,0}
};


MYSQL *conn; // pointer to connection handler


int start(char *host_name, char *user_name, char *password, char *db_name, unsigned int port_num, char *socket_name, unsigned int flags)
{
cout << "at Start" << endl;

if(host_name != NULL) cout << host_name << endl;
if(user_name != NULL) cout << user_name << endl;
if(password != NULL) cout << password << endl;
if(db_name != NULL) cout << db_name << endl;
if(port_num != NULL) cout << port_num << endl;
if(socket_name != NULL) cout << socket_name << endl;

if(pCmd->m_szhost_name != NULL) cout << pCmd->m_szhost_name_1 =" << pCmd->m_szhost_name << endl;

cout << "end start" << endl;

conn = do_connect(host_name, user_name, password, db_name, port_num, socket_name, 0);


if(conn == NULL)
return 1;

//do real work here

do_disconnect(conn);
return 0;
// return 0;
}



int main (int argc, char *argv[])
{
char *host_name = def_host_name;
char *user_name = def_user_name;
char *password = def_password;
unsigned int port_num = def_port_num;
char *socket_name = def_socket_name;
char *db_name = def_db_name;
char passbuf[100];
int ask_password = 0;
int i;
int c, option_index;

my_init();

CCommand *pCmd;

// load_defaults("my", groups, &argc, &argv); //removed because will not be using conf file

while((c =getopt_long (argc, argv, "h:p:u:p:S:", long_options, &option_index)) != EOF)
{
switch (c)
{
case 'h':
host_name = optarg;
cout << "host_name_ =" << host_name << endl;

strcpy(pCmd->m_szhost_name,host_name);
cout << "pCmd->m_szhost_name =" << pCmd->m_szhost_name << endl;

break;

case 'u':
user_name = optarg;
break;

case 'p':
if(!optarg) //no value given
ask_password = 1;
else
{
(void) strncpy (passbuf, optarg, sizeof(passbuf) -1);
passbuf[sizeof(passbuf) -1] = '\0';
password = passbuf;
while (*optarg)
*optarg ++ = ' ';
}
break;

case 'P':
port_num = atoi (optarg);
break;

case 'S':
socket_name = optarg;
break;
}
}

argc -= optind; //advance past the arguments that were processed
argv += optind; //by getopt_long

if(argc > 0)
{
db_name = argv[0];
--argc; ++argv;
}

if(ask_password)
password = get_tty_password(NULL);

start(host_name, user_name, password, db_name, port_num, socket_name, 0);


return 0;
}

the code does not compile because of the print statement "if(pCmd->m_szhost_name != NULL) cout << pCmd->m_szhost_name_1 =" << pCmd->m_szhost_name << endl;

cout << "end start" << endl;"

in function int start(

How would I get at the structure data entered in the main function using the start function.

Thanks in advance
 
I corrected the problem I had an error in the header file.
 
Just a minor point: if you are coding in C++, you don't have to use typedefs. If it makes typing easier or you have to interface with C programs then by all means but not for redefining structure names. The structure in the header file could be coded as
Code:
class CCommand
{
public:
        char m_szhost_name[MAXINPUT];
        char m_szuser_name[MAXINPUT];
        char m_zpassword[MAXINPUT];
        char m_zdb_name[MAXINPUT];
        unsigned int m_nport_num;
        char m_zsocket_name[MAXINPUT];
        unsigned int flags;
};
In C, many people typedef things so that you can write
Code:
CCommand xxx;

instead of 

struct CCommand xxx;
But in C++, you can write CCommand xxx anyway without the typedef. This applies to structs, unions and enums as well.
 
1. Pass pointer to or reference to a structure via function parameters:
Code:
struct CCommand
{
};
...
bool DoCmd(CCommand* pcmd);
bool DoCmd(CCommand& cmd);
...
CCommand cmd1; // Don't forget to initialize...
...
if (DoCmd(cmd1)) ...

2. struct Name == class Name { public: in C++...

3. Don't use old NULL macros in so undisciplined manner. Better forget it, use clear and precise null constant (digit 0 in C++;)...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top