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 equivalent 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 ) ; }
Angular is supported by all the popular mobile browsers. ng-bind is used to bind data from view to model and vice versa. Properties enclosed in “()” and “[]” are used to bind data between view and model.
Binding to a propertylink In this example, src is the name of the <img> element property. The brackets, [] , cause Angular to evaluate the right-hand side of the assignment as a dynamic expression.
The square brackets specify which variables are "captured" by the lambda, and how (by value or reference). Capture means that you can refer to the variable outside the lambda from inside the lambda.
They are used with many different things including classes, functions, loops, and conditionals. Square brackets are used to index (access) elements in arrays and also Strings. Specifically lost[i] will evaluate to the ith item in the array named lost.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With