I have a function in C which takes an array of structs as an argument:
int load_ini_parms(char * ini_file,
struct test_object s[],
int num,
struct lwm2m_object * l)
My question is, would it be better programming practice to pass a pointer to an array of structs? What would be the advantages or disadvantages?
It doesn't make any difference because passing an array as a function argument decays into a pointer .
The function
int load_ini_parms(char *ini_file, struct test_object s[], int num, struct lwm2m_object *l)
is equivalent to
int load_ini_parms(char *ini_file, struct test_object *s, int num, struct lwm2m_object *l)
The information regarding to the size of the array is lost in both cases.
Contrary to what others here say, I say it does make a difference as to what may be a "better programming practice":
Declaring the parameter as an array indicates to the user of the function that you expect, well, an array .
If you only declare a pointer this does not tell anyone if you expect a single element or possibly multiple.
Hence, I'd prefer the pointer for single-element parameters (eg "output" parameters) and the array for (potentially) multiple elements.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.