Difference between [square brackets] and *asterisk

23,786

Solution 1

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!

Solution 2

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]

Solution 3

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.

Solution 4

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.

Share:
23,786
bobobobo
Author by

bobobobo

Updated on November 25, 2020

Comments

  • bobobobo
    bobobobo over 3 years

    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 ) ;
    }
    
  • tloach
    tloach over 14 years
    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)
  • bobobobo
    bobobobo over 14 years
    in readEmSquare, checking sizeof( arrayOfInt ) will return 4, it is a pointer
  • Brian R. Bondy
    Brian R. Bondy over 14 years
    See my note about when you use char[] without initialization.
  • bobobobo
    bobobobo over 14 years
    :) - so wrt to style, does it matter/which as an argument to a function?
  • Brian R. Bondy
    Brian R. Bondy over 14 years
    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
    Brian R. Bondy over 14 years
    Also added a style section to my answer.
  • UncleBens
    UncleBens over 14 years
    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.
  • josesuero
    josesuero over 14 years
    @tloach: Theres no "stack vs heap" difference. A pointer doesn't have to point to the heap.
  • Steven Keith
    Steven Keith over 14 years
    I'll stick my neck out, and agree :)
  • bobobobo
    bobobobo over 14 years
    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.
  • Flimm
    Flimm over 9 years
    Declaring char example[10]; in a function doesn't initialise it, but it is an array, not a pointer. So the dividing line isn't whether it is initialised or not. The dividing line is whether the variable is declared as function parameter or not.