Difference between [square brackets] and *asterisk
If you write a C++ function like
void readEmStar( int *arrayOfInt ) { }
vs a C++ function like:
void readEmSquare( int arrayOfInt[] ) { }
What is the difference between using [square brackets] vs *asterisk, and does anyone have a style guide as to which is preferrable, assuming they are equi开发者_StackOverflow中文版valent to the compiler?
For completeness, an example
void readEmStar( int *arrayOfInt, int len )
{
for( int i = 0 ; i < len; i++ )
printf( "%d ", arrayOfInt[i] ) ;
puts("");
}
void readEmSquare( int arrayOfInt[], int len )
{
for( int i = 0 ; i < len; i++ )
printf( "%d ", arrayOfInt[i] ) ;
puts("");
}
int main()
{
int r[] = { 2, 5, 8, 0, 22, 5 } ;
readEmStar( r, 6 ) ;
readEmSquare( r, 6 ) ;
}
When you use the type char x[]
instead of char *x
without initialization, you can consider them the same. You cannot declare a new type as char x[]
without initialization, but you can accept them as parameters to functions. In which case they are the same as pointers.
When you use the type char x[]
instead of char *x
with initialization, they are completely 100% different.
Example of how char x[]
is different from char *x
:
char sz[] = "hello";
char *p = "hello";
sz
is actually an array, not a pointer.
assert(sizeof(sz) == 6);
assert(sizeof(sz) != sizeof(char*));
assert(sizeof(p) == sizeof(char*));
Example of how char x[]
is the same as char *x
:
void test1(char *p)
{
assert(sizeof(p) == sizeof(char*));
}
void test2(char p[])
{
assert(sizeof(p) == sizeof(char*));
}
Coding style for passing to functions:
It really doesn't matter which one you do. Some people prefer char x[]
because it is clear that you want an array passed in, and not the address of a single element.
Usually this is already clear though because you would have another parameter for the length of the array.
Further reading:
Please see this post entitled Arrays are not the same as pointers!
C++ Standard 13.1.3
— Parameter declarations that differ only in a pointer * versus an array [] are equivalent. That is, the array declaration is adjusted to become a pointer declaration (8.3.5). Only the second and subsequent array dimensions are significant in parameter types (8.3.4). [Example:
int f(char*);
int f(char[]); // same as f(char*);
int f(char[7]); // same as f(char*);
int f(char[9]); // same as f(char*);
int g(char(*)[10]);
int g(char[5][10]); // same as g(char(*)[10]);
int g(char[7][10]); // same as g(char(*)[10]);
int g(char(*)[20]); // different from g(char(*)[10]);
—end example]
There is no difference between your two codes, apart from the different style obviously. In both cases the array is passed by reference and not by value, as function parameters type *x
and type x[]
are semantically the same.
On the style question I'll stick my neck out and say int *arrayOfInt is better. Which ever syntax you use you are passing a pointer and the type should make that clear.
This is just my opinion.
The two expressions are equivalent. They each evaluate to the address of the first element of the array arrayOfInt.
精彩评论