C compound literals, pointer to arrays

前端 未结 5 1824
南笙
南笙 2020-12-13 13:12

I\'m trying to assign a compound literal to a variable, but it seems not to work, see:

  int *p[] = (int *[]) {{1,2,3},{4,5,6}};

I got a er

相关标签:
5条回答
  • 2020-12-13 13:52

    First understand that "Arrays are not pointers".

    int p[] = (int []) {1,2,3,4,5,6};
    

    In the above case p is an array of integers. Copying the elements {1,2,3,4,5,6} to p. Typecasting is not necessary here and both the rvalue and lvalue types match which is an integer array and so no error.

    int *p[] = (int *[]) {{1,2,3},{4,5,6}};
    

    "Note I don't understand why I got a error in the first one,.."

    In the above case, p an array of integer pointers. But the {{1,2,3},{4,5,6}} is a two dimensional array ( i.e., [][] ) and cannot be type casted to array of pointers. You need to initialize as -

    int p[][3] = { {1,2,3},{4,5,6} };
      // ^^ First index of array is optional because with each column having 3 elements
      // it is obvious that array has two rows which compiler can figure out.
    

    But why did this statement compile ?

    char *p[] = {"one", "two"...};
    

    String literals are different from integer literals. In this case also, p is an array of character pointers. When actually said "one", it can either be copied to an array or point to its location considering it as read only.

    char cpy[] = "one" ;
    cpy[0] = 't' ;  // Not a problem
    
    char *readOnly = "one" ;
    readOnly[0] = 't' ;  // Error because of copy of it is not made but pointing
                         // to a read only location.
    

    With string literals, either of the above case is possible. So, that is the reason the statement compiled. But -

    char *p[] = {"one", "two"...}; // All the string literals are stored in 
                                   // read only locations and at each of the array index 
                                   // stores the starting index of each string literal.
    

    I don't want to say how big is the array to the compiler.

    Dynamically allocating the memory using malloc is the solution.

    Hope it helps !

    0 讨论(0)
  • 2020-12-13 13:58

    Since nobody's said it: If you want to have a pointer-to-2D-array, you can (probably) do something like

    int (*p)[][3] = &(int[][3]) {{1,2,3},{4,5,6}};
    

    EDIT: Or you can have a pointer to its first element via

    int (*p)[3] = (int[][3]) {{1,2,3},{4,5,6}};
    

    The reason why your example doesn't work is because {{1,2,3},{4,5,6}} is not a valid initializer for type int*[] (because {1,2,3} is not a valid initializer for int*). Note that it is not an int[2][3] — it's simply an invalid expression.

    The reason why it works for strings is because "one" is a valid initializer for char[] and char[N] (for some N>3). As an expression, it's approximately equivalent to (const char[]){'o','n','e','\0'} except the compiler doesn't complain too much when it loses constness.

    And yes, there's a big difference between an initializer and an expression. I'm pretty sure char s[] = (char[]){3,2,1,0}; is a compile error in C99 (and possibly C++ pre-0x). There are loads of other things too, but T foo = ...; is variable initialization, not assignment, even though they look similar. (They are especially different in C++, since the assignment operator is not called.)

    And the reason for the confusion with pointers:

    • Type T[] is implicitly converted to type T* (a pointer to its first element) when necessary.
    • T arg1[] in a function argument list actually means T * arg1. You cannot pass an array to a function for Various Reasons. It is not possible. If you try, you are actually passing a pointer-to-array. (You can, however, pass a struct containing a fixed-size array to a function.)
    • They both can be dereferenced and subscripted with identical (I think) semantics.

    EDIT: The observant might notice that my first example is roughly syntactically equivalent to int * p = &1;, which is invalid. This works in C99 because a compound literal inside a function "has automatic storage duration associated with the enclosing block" (ISO/IEC 9899:TC3).

    0 讨论(0)
  • 2020-12-13 14:02

    First, the casts are redundant in all of your examples and can be removed. Secondly, you are using the syntax for initializing a multidimensional array, and that requires the second dimension the be defined in order to allocate a sequential block of memory. Instead, try one of the two approaches below:

    • Multidimensional array:

      int p[][3] = {{1,2,3},{4,5,6}};
      
    • Array of pointers to one dimensional arrays:

      int p1[] = {1,2,3};
      int p2[] = {4,5,6};
      int *p[] = {p1,p2};
      

    The latter method has the advantage of allowing for sub-arrays of varying length. Whereas, the former method ensures that the memory is laid out contiguously.

    Another approach that I highly recommend that you do NOT use is to encode the integers in string literals. This is a non-portable hack. Also, the data in string literals is supposed to be constant. Do your arrays need to be mutable?

    int *p[] = (int *[]) {
        "\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00",
        "\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00"
    };
    

    That example might work on a 32-bit little-endian machine, but I'm typing this from an iPad and cannot verify it at the moment. Again, please don't use that; I feel dirty for even bringing it up.

    The casting method you discovered also appears to work with a pointer to a pointer. That can be indexed like a multidimensional array as well.

    int **p = (int *[]) { (int[]) {1,2,3}, (int[]) {4,5,6} };
    
    0 讨论(0)
  • 2020-12-13 14:13

    The one that you are using is array of int pointers. You should use pointer to array :

    int (*p)[] = (int *) {{1,2,3}, {4,5,6}}
    

    Look at this answer for more details.

    0 讨论(0)
  • 2020-12-13 14:13

    It seems you are confusing pointers and array. They're not the same thing! An array is the list itself, while a pointer is just an address. Then, with pointer arithmetic you can pretend pointers are array, and with the fact that the name of an array is a pointer to the first element everything sums up in a mess. ;)

    int *p[] = (int *[]) {{1,2,3},{4,5,6}};      //I got a error
    

    Here, p is an array of pointers, so you are trying to assign the elements whose addresses are 1, 2, 3 to the first array and 4, 5, 6 to the second array. The seg fault happens because you can't access those memory locations.

    int p[][3] = {{1,2,3},{4,5,6}};              //it's okay
    

    This is ok because this is an array of arrays, so this time 1, 2, 3, 4, 5 and 6 aren't addresses but the elements themselves.

    char *p[] = (char *[]) {"one", "two"...};    // it's okay!
    

    This is ok because the string literals ("one", "two", ...) aren't really strings but pointers to those strings, so you're assigning to p[1] the address of the string literal "one".
    BTW, this is the same as doing char abc[]; abc = "abc";. This won't compile, because you can't assign a pointer to an array, while char *def; def = "def"; solves the problem.

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