malloc()
によって返されたポインタを多次元配列の名前として使いたいので、ポインタをキャストします。コンパイルすると、私はエラーを得た:なぜ同じタイプのレポートが互換性がないのですか?
clang++ -o cast cast.cpp
cast.cpp:29:12: error: assigning to 'int8_t (*)[width]' from incompatible type
'int8_t (*)[width]'
int8line = (int8_t (*)[width])(array);
^~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
しかし、Linux上で同じプラットフォーム(OS用X)上のicpc
とg++/icpc
でコンパイル、エラーがスローされません。
更新:幅が可変であるため、const
にできません。
テストしたコンパイラは、次のとおり
Mac OS X
icpc (ICC) 17.0.4 20170411 clang --version Apple LLVM version 8.1.0 (clang-802.0.42) Target: x86_64-apple-darwin16.6.0 Thread model: posix
Linux 2.6.32:
icpc (ICC) 14.0.0 20130728 g++ (GCC) 4.4.6 20110731
結果がなければならない:
icpc -std=c++11 -o cast cast.cpp && ./cast 5
0 1 2 3 4
5 6 7 8 9
0 1 2 3 4
5 6 7 8 9
最小コンパイル可能コード(更新):
#include <iostream>
#include <cstdint>
using namespace std;
int main(int argc, char* argv[])
{
if (argc != 2)
{
cerr << "needs one argument\n";
exit(EXIT_FAILURE);
}
long width = atol(argv[1]);
int8_t *array = static_cast<int8_t(*)>(malloc(sizeof(int8_t)*2*width));
for (int i = 0; i < 2*width; i++)
array[i] = i;
for (int j = 0; j < 2; j++)
{
for (int i = 0; i < width; i++)
{
cout << static_cast<int>(array[i+j*width]);
if (i < (width-1))
cout << '\t';
}
cout << '\n';
}
cout << '\n';
int8_t (*int8line)[width];
int8line = (int8_t (*)[width])(array);
for (int j = 0; j < 2; j++)
{
for (int i = 0; i < width; i++)
{
cout << static_cast<int>(int8line[j][i]);
if (i < (width-1))
cout << '\t';
}
cout << '\n';
}
free(array);
exit(EXIT_SUCCESS);
}
'long width = 5;' widthはconstにする必要があります – Loreto
C++で 'malloc'を使用するのは非常に奇妙で、C言語では* cast *と同じくらい奇妙です。これはあなたの質問が[XY問題](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)であり、X部分(問題の最も重要な部分)に対する答えは、 'new int8_t [width]'または 'std :: vector'を使用します。 –
Sebivor
[This(http://coliru.stacked-crooked.com/a/fd11dba2af9b5213)はGCCではコンパイルされますが、clangではコンパイルされません。それは、clangが可変長配列へのポインタを受け付けない理由です。 –