Thread: Help with a Struct problem

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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.
    Last edited by mike_g; 10-01-2008 at 05:15 AM.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok, here is an analogous situation.

    Brownie lets pretend like we are mechanics for my hypothetical.

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct File and array problem Please help
    By theprogrammer in forum C Programming
    Replies: 17
    Last Post: 04-02-2009, 08:05 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Replies: 22
    Last Post: 12-23-2008, 01:53 PM
  5. Replies: 16
    Last Post: 10-29-2006, 05:04 AM

Tags for this Thread