Definition type

In short, I have no idea...
Which way is faster?
1
2
3
Func(char *str, double d, int len, bool bExtra)
Func(char str[],double d, short len, bool bExtra)
Func(char str[], double *d, short len, bool bExtra);
Last edited on
There is no difference between the parameter declarations

char *str and char str[] because an array declared as passed by value implicitly is converted to a pointer to its first element. So these two declarations are equivalent.

As for double and double * then double * adds additional indirect access that requires more mashine commands. As for int and short then short usually is promoted to int. So it is better to use int.
There is no difference between the parameter declarations

There is a difference between a pointer to a character (L1) and an array of char (L2 and L3)
In L3 d is a pointer to double but in the other 2 lines d is a double type variable.
The first two are not declarations as the (;) is missing. So the third and only prototype is the only one which can be expanded into a valid definition. It must be the fastest!











Last edited on
There is a difference between a pointer to a character (L1) and an array of char (L2 and L3)
You are correct that there is a difference between a pointer to a char and an array of char but in the example above str is a pointer in all three lines.
Last edited on
Yes Peter87 but in the case of the last two, str operate as pointers to the first element of each of their respective arrays. In the first case *ptr is a pointer to a unknown char (but existing to be legal!)... and in my view there-in lies the difference.
Please excuse my pedantisism
Func(char *str, double *d, int len, bool bExtra) should be better
Func(char *str, double *d, short len, bool bExtra) should be better
Yes Peter87 but in the case of the last two, str operate as pointers to the first element of each of their respective arrays. In the first case *ptr is a pointer to a unknown char (but existing to be legal!)...


They don't just operate as pointers. They literally are pointers. The array isn't copied and passed to the function. You can't use sizeof() on the parameter and get the size of the original array. Assigning to the pointer doesn't affect the original array. (And then there's the fact that you can assign to the pointer and that's something you couldn't do to an array...)

func( nullptr )

is just as legal for
void func(char* c);
as it is for
void func(char c[]);

What's more, if the two were different types you could overload the function based on the types. The types are identical, though, so that's impossible.

Last edited on
@buffbill (403)
There is no difference between the parameter declarations

There is a difference between a pointer to a character (L1) and an array of char (L2 and L3)


You are wrong. All the following function declarations are equivalent and declare the same function

void f( char * );
void f( char [] );
void f( char [10] );
void f( char [20] );
Topic archived. No new replies allowed.