Search code examples
carrayspointersinitializationnotation

Initializing an array using pointer notation


Is it possible to initialize an array using pointer notation from another array?

To clarify, if you can use sizeof(*table) to return the size of a row, why does it not work to assign a row of a specific table? Is there some way to use double table2[3] = *table; to assign only the first row of table to table2.

#include <stdio.h>

int main()
{
    double table[2][3] = {{1.1, 1.2, 1.3},{2.1, 2.2, 2.3}}; 
    double table2[2][3]=table;

    // initial variant was double table2[3]=*table;
}

Solution

  • Is it possible to initialize an array using pointer notation from another array?

    No, it is not. The standard specifies that

    [...] the initializer for an object that has aggregate or union type shall be a brace-enclosed list of initializers for the elements or named members.

    (C2011, 6.7.9/16)

    Moreover, note that such an initializer is not an array literal (those have a slightly different form, and are not allowed as array initializers), and that although arrays and pointers have a close association, they are completely different things. You cannot anywhere assign a pointer (or anything else) to a whole array.