pointer to array c++

后端 未结 3 1637
鱼传尺愫
鱼传尺愫 2020-11-28 06:52

What is the following code doing?

int g[] = {9,8};
int (*j) = g;

From my understanding its creating a pointer to an array of 2 ints. But th

相关标签:
3条回答
  • 2020-11-28 07:27
    int g[] = {9,8};
    

    This declares an object of type int[2], and initializes its elements to {9,8}

    int (*j) = g;
    

    This declares an object of type int *, and initializes it with a pointer to the first element of g.

    The fact that the second declaration initializes j with something other than g is pretty strange. C and C++ just have these weird rules about arrays, and this is one of them. Here the expression g is implicitly converted from an lvalue referring to the object g into an rvalue of type int* that points at the first element of g.

    This conversion happens in several places. In fact it occurs when you do g[0]. The array index operator doesn't actually work on arrays, only on pointers. So the statement int x = j[0]; works because g[0] happens to do that same implicit conversion that was done when j was initialized.

    A pointer to an array is declared like this

    int (*k)[2];
    

    and you're exactly right about how this would be used

    int x = (*k)[0];
    

    (note how "declaration follows use", i.e. the syntax for declaring a variable of a type mimics the syntax for using a variable of that type.)

    However one doesn't typically use a pointer to an array. The whole purpose of the special rules around arrays is so that you can use a pointer to an array element as though it were an array. So idiomatic C generally doesn't care that arrays and pointers aren't the same thing, and the rules prevent you from doing much of anything useful directly with arrays. (for example you can't copy an array like: int g[2] = {1,2}; int h[2]; h = g;)


    Examples:

    void foo(int c[10]); // looks like we're taking an array by value.
    // Wrong, the parameter type is 'adjusted' to be int*
    
    int bar[3] = {1,2};
    foo(bar); // compile error due to wrong types (int[3] vs. int[10])?
    // No, compiles fine but you'll probably get undefined behavior at runtime
    
    // if you want type checking, you can pass arrays by reference (or just use std::array):
    void foo2(int (&c)[10]); // paramater type isn't 'adjusted'
    foo2(bar); // compiler error, cannot convert int[3] to int (&)[10]
    
    int baz()[10]; // returning an array by value?
    // No, return types are prohibited from being an array.
    
    int g[2] = {1,2};
    int h[2] = g; // initializing the array? No, initializing an array requires {} syntax
    h = g; // copying an array? No, assigning to arrays is prohibited
    

    Because arrays are so inconsistent with the other types in C and C++ you should just avoid them. C++ has std::array that is much more consistent and you should use it when you need statically sized arrays. If you need dynamically sized arrays your first option is std::vector.

    0 讨论(0)
  • 2020-11-28 07:33

    The parenthesis are superfluous in your example. The pointer doesn't care whether there's an array involved - it only knows that its pointing to an int

      int g[] = {9,8};
      int (*j) = g;
    

    could also be rewritten as

      int g[] = {9,8};
      int *j = g;
    

    which could also be rewritten as

      int g[] = {9,8};
      int *j = &g[0];
    

    a pointer-to-an-array would look like

      int g[] = {9,8};
      int (*j)[2] = &g;
    
      //Dereference 'j' and access array element zero
      int n = (*j)[0];
    

    There's a good read on pointer declarations (and how to grok them) at this link here: http://www.codeproject.com/Articles/7042/How-to-interpret-complex-C-C-declarations

    0 讨论(0)
  • j[0]; dereferences a pointer to int, so its type is int.

    (*j)[0] has no type. *j dereferences a pointer to an int, so it returns an int, and (*j)[0] attempts to dereference an int. It's like attempting int x = 8; x[0];.

    0 讨论(0)
提交回复
热议问题