-1

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;
}
Eric
  • 59
  • 6
  • 3
    Not clear what you want. There is no "pointer notation" for arrays. Arrays are **not** pointers - both are different datatypes! – too honest for this site Dec 06 '16 at 16:24
  • Well, try it. Does it compile? – MD XF Dec 06 '16 at 16:27
  • 1
    No, arrays cannot be assigned to. You could `memcpy()` the contents of one array to the other though. – EOF Dec 06 '16 at 16:28
  • Thank you EOF. I think that might be what is happening, it is trying to insert an entire row of a table into a single element of a secondary, because assignment isn't defined. (Is that correct?) – Eric Dec 06 '16 at 16:29
  • https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp4_PointerReference.html There should be a way to remove a useless comment Olaf. A quick google search reveals what "pointer notation for C arrays means" Whoever downscored this should also have a point of reputation removed. I was a newb and trying to learn. – Eric Jan 12 '18 at 15:48

1 Answers1

0

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157