C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-01-2008, 04:52 AM   #1
Registered User
 
Join Date: Feb 2006
Location: Ballincollig, co. cork, Eire
Posts: 19
Help with a Struct problem

Hi,

Just need a bit of advice on some C++ related issues. I have the following:

Code:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <windows.h>
#include "et1000.h"
#pragma comment( lib, "smbw32vc.lib" )

using namespace std;

//prototypes
// h = Hub, s = Slot, p = Port
void setTxParams (int h, int s, int p);

int main()
{
	//Set Hub, Slot and Ports to be used.
	int h1 = 0;
	int s1 = 0;
	int p1 = 0;

	int h2 = 0;
	int s2 = 0;
	int p2 = 1;

	unlink();

	return 0;
}

void setTxParams (int h, int s, int p)
{
	CHECKERROR(HTSetStructure(ETH_TRANSMIT, 0, 0, 0, 
		(void*) pETHTransmit, sizeof(ETHTransmit), h, s, p));
}

typedef struct tagETHTransmit
{
	unsigned char ucTransmitMode; /* transmit mode:
	CONTINUOUS_PACKET_MODE
	SINGLE_BURST_MODE
	MULTI_BURST_MODE
	CONTINUOUS_BURST_MODE
	ECHO_MODE */
	unsigned short uiDataLength; /* number of bytes per frame, not
	including 4 byte CRC */
} ETHTransmit;
First, the code is not complete (especially int main()), but there is enough for you to answer my question.

Is it ok to put the struct at the end of the code, like above? If not, where should I put it?
I have a lot of parameters that need to be specified to set up the transmit parameters. void setTxParams points to the struct. So, where do I set the values for the struct parameters?
In int main() like this:
Code:
ETHTransmit.ucTransmitMode = SINGLE_BURST_MODE;
or in void setTxParams(...) like this:
Code:
void setTxParams (int h, int s, int p)
{
ETHTransmit.ucTransmitMode = SINGLE_BURST_MODE;
	CHECKERROR(HTSetStructure(ETH_TRANSMIT, 0, 0, 0, 
		(void*) pETHTransmit, sizeof(ETHTransmit), h, s, p));
}

Finally, is there anything more I may need to do that I may have left out?

Regards

Brownie

//Using Microsoft Visual C++ Express 2008 on a Windows XP machine
Brownie is offline   Reply With Quote
Old 10-01-2008, 05:01 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,364
Quote:
Is it ok to put the struct at the end of the code, like above? If not, where should I put it?
Generally, struct and class definitions are placed in header files, so after preprocessing they would be near the top of code that includes the given header. If you do not place the struct definition in a header, then you should place the struct definition near the top, or at least before the definition of any function that uses the struct. In some cases you do not even need the struct definition at all, and a struct declaration (a.k.a. forward declaration) will do. I do not think that this applies here, since you use sizeof(ETHTransmit), and in order for the size of the struct to be known, its definition must be known.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 10-01-2008, 05:12 AM   #3
Wheres the lesbians?
 
mike_g's Avatar
 
Join Date: Oct 2006
Location: UK
Posts: 1,219
Quote:
Is it ok to put the struct at the end of the code, like above? If not, where should I put it?
No, your struct declaration should go in a header file or at the top of you program.

AFAIK the declaration does not create a struct it simply defines the stucture and the way its laid out so each time that you want to create an instance of ETHTransmit you will need to declare it as:
Code:
ETHTransmit instanceName;
Where instanceName is the name you give the variable. To set a field within an instance of your stuct you can then do something like:
Code:
instanceName.ucTransmitMode = SINGLE_BURST_MODE;
As you are using C++ it might want to consider using classes instead of stucts if possible.
__________________
Senior highbrow doctor of authority.

Last edited by mike_g; 10-01-2008 at 05:15 AM.
mike_g is offline   Reply With Quote
Old 10-01-2008, 11:58 AM   #4
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
Ok, here is an analogous situation.

Brownie lets pretend like we are mechanics for my hypothetical.

Quote:
Hey Brownie today we are going to use the new cryoplasmic reverse-syphoning spinner rig. How would you rate its performance?
That is actually more analogous to giving a foreward definition of a struct (a prototype) and then not defining it before using it. You obviously cannot do much more than identify that the tool I made up exists. Beyond that you could not describe what it does, how it works, or how well it works. If your code were my hypothetical scenario I would merely just ask the question about performance without even pointing out that the tool existed first.

Does that make sense? I could argue that a compiler could be written to preprocess an entire project and scan for all formal class and structure definitions prior to trying to build. But C/C++ do not do that, and its a simple rule to follow. If programming were about wishful thinking, I wish my compiler would automatically build a production build whenever there were no bugs. And if there are bugs, I wish it would automatically place breakpoints immediately before them.
master5001 is offline   Reply With Quote
Reply

Tags
struct

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Struct File and array problem Please help theprogrammer C Programming 17 04-02-2009 08:05 AM
memory issue t014y C Programming 2 02-21-2009 12:37 AM
Link List math t014y C Programming 17 02-20-2009 06:55 PM
problem with reading struct from file generated by hexdump wollek C Programming 22 12-23-2008 01:53 PM
Converting a circulating mouse pointer to use a Potentionmeter phoenix23 C Programming 16 10-29-2006 05:04 AM


All times are GMT -6. The time now is 05:57 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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