Thread: Passing structures to a function

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Passing structures to a function

    I have a class that contains a vector of structs. I want to pass each of the structs in the array to a function one by one. I get a run-time assertion error.

    Here are important (I think) bits of the code
    Code:
    // function that uses the struct:
    int MakeArincWordAnalog(DataLine *Line, float value,  int word)
    {
    	int  number;
    
    	word =0;
    
    	FILE *out;
        out = fopen("Victor - test.txt", "w");
    
    	fprintf(out, ">%s %d %d %d %d %d %d %d\n<", Line->Param, Line->Label,
                        Line->SFactor, Line->SigBits, Line->Bit30,
    					Line->Bit31, Line->Max, Line->Min);
    
    // The function call in main:
    MakeArincWordAnalog(&FAD.vectDL[i], mtxVicVals[j][i], TempWord);
    
    // the struct and class declarations:
    struct DataLine
    {
    	char Param[20];
    	int Label, SFactor, SigBits;
    	int Bit30, Bit31, Max, Min;
    
    	DataLine()
    	{
    		Label=SFactor=SigBits=Bit30=Bit31=Max=Min=0;
    		memset(Param, 0, sizeof(Param));
    	};
    
    } ;
    
    class FSAD        //Fixed/Selectable Analog Data  
    	{
    	public:
    
    		char Name[25];
    		int nExpected;  // size = nExected*2+1
    		std::vector < DataLine > vectDL;		
    	};
    The code doesn't seem to like the way I pass it the struct.
    Last edited by earth_angel; 07-12-2005 at 01:08 PM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, from what I see, you have DataLine defined under main, and the function defined above main... this is a problem... you need to at least put a prototype of the struct above where you defined the function so the function knows what to expect...

    I haven't looked too far into it, but try that and see if it works...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That code looks fine from the vector perspective. Are you sure i is a valid index to the vector? Also, why are you passing a pointer? Just pass it by value.

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I actually screwed up the way I worked with my files.
    The rest of the def's was fine.
    here's the whole function:
    Code:
    int MakeWord(FILE *out, DataLine *Line, float value,  int word)
    {
    	int  number;
    
    	word =0;
    
    	/*fprintf(out, ">%s %d %d %d %d %d %d %d\n<", Line->Param, Line->Label,
                        Line->SFactor, Line->SigBits, Line->Bit30,
    					Line->Bit31, Line->Max, Line->Min);*/
    
    	// check if the value is valid
    	if(value == -9999)
    		return 0;
    
    	//Input the label
    	word = MakeLabel(Line->Label);
    	fprintf(out, " label %08x \n", word);
    
    	//Check if value is negative
    	if(value < 0)
    	{
    		word = word | 0x10000000; // Set the sign bit to 1
    		value = -value ;
    	}
    
    	// Set bits 30 
    	if (Line->Bit30 == 1)
    		word = word | 0x20000000;  
    	else
    	{
    		word = word << 3;   
    		word = word >> 3;
    	}
    
    	//and bit 31
    	if (Line->Bit31 ==1)
    		word = word | 0x40000000;
    	else
    	{
    		word = word << 2;
    		word = word >> 2;
    	}
    
    	fprintf(out, " bits 30/31 %08x \n", word);
    
    	// Data part
    	if (Line->SFactor>0)
    	{
    		value = value*(float)pow(2,31)/Line->SFactor;
    		//printf("Value: %08x\n\n", value); 
    		number = ((int)value)>>3;  
    		//printf("Number: %08x\n\n", number);
    	}
    
    	word = word | (number & DATAMASK18);
    
    	fprintf(out, " data %08x \n", word);
    
    	// Set parity to 0
    	word = word << 1;
    	word = word >> 1;
    
    	fprintf(out, " parity %08x \n", word);
    
    	return 1;
    	
    	
    }
    and it's called like this:
    Code:
    MakeWord(out, &FAD.vectDL[i],
    				Value, TempWord);
    when I try to print TempWord after the function call it's empty, but inside the function word is adjusted and the last fprintf states the correct result. Why doesn't it reflect the correct value for TempWord?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You are passing TempWord by value. If you want it to be updated when it is modified inside the function, the function should take a reference (for example, int& word). You could also have it take a pointer, but that would require more changes to the function code and the calling code.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Yep, I arrived at the same conclussion. So that part is fixed now.
    As always a new problem rose: I have two return statements: One toreturn 0 if the ntry is invalid and the other return 1 if it's all good. I want to use it to check whether I want it to use the value produced by the function or not. For some reason I get a run-time error saying that the memory could not be read.

    Here's the if statement:
    Code:
    if(MakeWord(out, &FAD.vectDL[i], mtxVicVals[j][i], TempWord) == 1)
    and here's the new function declaration with the TempValue reference:
    Code:
    int MakeWord(FILE *out, DataLine *Line, float value,  int &word)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a byte and its bits to a function
    By rtarbell in forum C Programming
    Replies: 9
    Last Post: 12-04-2008, 09:24 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. Passing my array to function
    By pooty tang in forum C Programming
    Replies: 8
    Last Post: 09-15-2004, 12:19 PM