Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

junior C coding question?

Status
Not open for further replies.

mImoto

Full Member level 4
Joined
Feb 21, 2002
Messages
210
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,733
Hello,

I am using structs in my code. I don't know which is better or a more standar style about using functions with structs. In my case I pass by value to a function several bytes of a struct, not the entire struct. Do you seniors pass by value or by reference struct to functions when you don't need to pass much bytes?

i.e.

PORTS_PINS_Write_Port(MessageRx.BYTES.BYTE_4,
MessageRx.BYTES.BYTE_2);

thanks a lot and regards,

mimoto
 

It is more efficient to pass a pointer to the structure.
 

Pass a pointer to the structure and use whatever structure element you want.


typedef struct
{
/* define data here */
} structMessageRx;


int PORTS_PINS_Write_Port(structMessageRx *MessageRx)
{
x = MessageRx->BYTES.BYTE_4;
return 0;
}
 

Hi,

Using passage by reference allow you to pass all the structure to the function in one time.

More over it let you able to return an integer value at the end of the function to know if it ended normaly and have updated fields of the structure and use them in others functions.

Toto2001
 

Thanks for the help, I don't post this "thanks message" for points just to thank the quick answers to my question. Tomorrow I will change my code.

Best regards,

mimoto
 

Hello again,

Just one more question. What if I need also pass the struct by reference to another function inside the function to which I have passed it by reference. I mean I would like to pass a struct by reference to a function and then inside this function pass again the struct by reference to another function. How do you seniors do this? something like below?

typedef struct
{
/* define data here */
} structMessageRx;


int PORTS_PINS_Write_Port(structMessageRx *MessageRx)
{
x = MessageRx->BYTES.BYTE_4;
New_Function(MessageRx)

return 0;
}

New_Function(structMessageRx *MessageRx)
{
...
return 0;
}


Thanks in advance and Best regards,

mimoto
 

Yes, that is the way to do it. Your code seems correct.

Tom
 

Hi,
you are right. That's the way to do it.

Good Luck
HMaier
 

Last question I promise. If I use instead of struct unions¿?.

------------------------------
typedef union MESSAGE
{
unsigned char Data[8];

struct
{
BYTE BYTE_0;
BYTE BYTE_1;
BYTE BYTE_2;
BYTE BYTE_3;
BYTE BYTE_4;
BYTE BYTE_5;
BYTE BYTE_6;
BYTE BYTE_7;
} BYTES;

} RX_MESSAGE;


----------------------------------------

main{

RX_MESSAGE MessageRx1;

PORT_PINS_Write_Port(&MessageRx1)
...
}

-----------------------------------------------------------------
int PORT_PINS_Write_Port(RX_MESSAGE *MessageRx)
{

x = MessageRx->BYTES.BYTE_4;
y = NewFunction(MessageRx);
if (y |= 0)
{
return 1; //(*)question below
}
else return 0;
}

New_Function(RX_MESSAGE *MessageRx)
{
...
return 0;
}

Is this also the correct way for unions?
(*) How do you experts use the return values? this way?

Thanks in advance for your time,

Best regards,

mimoto
 

1. typedef defines new user type data. That includes structures, unions, etc ...

Passing pointer to some object type in some function call is OK, whatever that object may be as long as it is defined properly.

2.
/* 1st way */
If ( y != 0)
{
return 1;
}
else
{
return 0;
}

/* 2nd way */

return (y ? 1 : 0);

Tom
 

That is correct with unions and structures.
Pointers always have the same size (32 bits in Windows or less in embedded), so if this pointer points to a char you will send 4 bytes instead of 1, but if it points to a huge structure it will be 4 bytes long as well.
 

when you have many functions to which you should pass the same reference. Do you use the same name in the arguments of the function or different?

1st option:
Int PORT_PINS_Write_Port(RX_MESSAGE *MessageRx)
New_Function(RX_MESSAGE *MessageRx)
New_Function2(RX_MESSAGE *MessageRx)

2nd option:
Int PORT_PINS_Write_Port(RX_MESSAGE *MessageRx1)
New_Function(RX_MESSAGE *MessageRx2)
New_Function2(RX_MESSAGE *MessageRx3)

Thanks in advance for your time,

mimoto
 

It a question about coding style, not C language. I use same name in called functionas. More on coding style can be found on the Internet.

Tom
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top