LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing structures as function arguments versus pointers to structures

Does CVI copy the entire C structure when you pass it directly instead of passing a pointer to it?
0 Kudos
Message 1 of 6
(3,705 Views)
Yes, indeed.
 
Luis
NI
0 Kudos
Message 2 of 6
(3,686 Views)

It is almost always better to pass a pointer to your struct. Keep in mind:

  • By passing a pointer, you avoid the overhead of pushing the entire contents of the struct onto the stack, then popping it in your called function. This is especially a problem when dealing with large structs.
  • If your function needs to modify any member of the struct, you have no choice: you must pass a pointer.
Regards,
Colin.
 
0 Kudos
Message 3 of 6
(3,677 Views)
ANSI C (C89) provides ONLY call by value parameter passing, but that value can be a pointer 🙂  Array parameters are converted to pointers, and the pointer is then passed by value.

A struct is passed by value (not automatically converted to a pointer value), and you can of course modify such a struct in the called routine.  The issue is whether you intend changes to the struct made in the called routine to occur in the actual parameter (pointer to struct used as parameter value and explicitly dereferenced)  or in the local copy of a struct passed as a parameter.


0 Kudos
Message 4 of 6
(3,671 Views)
Thanks all, I suspected as much.

I have seen C compilers break the ANSI rule and pass a pointer when the function argument wasn't.
0 Kudos
Message 5 of 6
(3,663 Views)
My reply appeared earlier in the thread, thanks all.
0 Kudos
Message 6 of 6
(3,661 Views)