views:

274

answers:

5

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 ) ;
}
+12  A: 

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!

Brian R. Bondy
Since arrays and pointers are pretty similar in C++ perhaps you could add a quick bit on what the difference is (stack vs. heap, etc)
tloach
in readEmSquare, checking sizeof( arrayOfInt ) will return 4, it is a pointer
bobobobo
See my note about when you use char[] without initialization.
Brian R. Bondy
:) - so wrt to style, does it matter/which as an argument to a function?
bobobobo
No it doesn't matter. "When you use the type char[] instead of char* without initialization, you can consider them the same."
Brian R. Bondy
Also added a style section to my answer.
Brian R. Bondy
@tloach: Theres no "stack vs heap" difference. A pointer doesn't have to point to the heap.
jalf
+2  A: 

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.

Tom
Semantically the same only as function parameters.
David Thornley
An array is not passed here by reference (yes, in C++ it is also possible to pass arrays by reference). Instead the array, when passed to a function, decays to pointer to the first element, which is passed by value. No difference between the codes, though.
UncleBens
A: 

The two expressions are equivalent. They each evaluate to the address of the first element of the array arrayOfInt.

William Bell
+4  A: 

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]

Alexey Malistov
Good example! I've been wondering about that.. so the number is discarded by the compiler if you specify it, basically, unless its 2D or more, in which case, only the last number is kept.
bobobobo
+1  A: 

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.

Patrick
I'll stick my neck out, and agree :)
Steven
Guys your necks are showing
bobobobo