Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

junior C coding question?

Status
Not open for further replies.

mImoto

Full Member level 4
Joined
Feb 21, 2002
Messages
211
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,746
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
 

btbass

Advanced Member level 5
Joined
Jul 20, 2001
Messages
1,897
Helped
438
Reputation
880
Reaction score
287
Trophy points
1,363
Location
Oberon
Activity points
12,861
It is more efficient to pass a pointer to the structure.
 

tom324

Full Member level 1
Joined
Jun 26, 2001
Messages
98
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Croatia
Activity points
672
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;
}
 

ze_dib

Member level 5
Joined
Jan 6, 2003
Messages
80
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
France
Activity points
1,122
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
 

mImoto

Full Member level 4
Joined
Feb 21, 2002
Messages
211
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,746
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
 

mImoto

Full Member level 4
Joined
Feb 21, 2002
Messages
211
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,746
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
 

tom324

Full Member level 1
Joined
Jun 26, 2001
Messages
98
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Croatia
Activity points
672
Yes, that is the way to do it. Your code seems correct.

Tom
 

HMaier

Junior Member level 3
Joined
Dec 5, 2001
Messages
30
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
151
Hi,
you are right. That's the way to do it.

Good Luck
HMaier
 

mImoto

Full Member level 4
Joined
Feb 21, 2002
Messages
211
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,746
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
 

tom324

Full Member level 1
Joined
Jun 26, 2001
Messages
98
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Croatia
Activity points
672
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
 

gcolsani

Newbie level 6
Joined
Jan 10, 2003
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Córdoba - Argentina
Activity points
117
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.
 

mImoto

Full Member level 4
Joined
Feb 21, 2002
Messages
211
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,746
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
 

tom324

Full Member level 1
Joined
Jun 26, 2001
Messages
98
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,288
Location
Croatia
Activity points
672
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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top