如果我將第二個調用添加到setColor,我得到一個分段錯誤。我想也許這種方法以某種方式改變數組指針,但我不知道爲什麼。第二次Segmentaion故障調用方法C++
#include <iostream>
using namespace std;
struct Color {
int red;
int blue;
int green;
};
void setColor(Color **arr, int index, int red, int blue, int green) {
Color *ptr = arr[index];
(*ptr).red = red;
(*ptr).blue = blue;
(*ptr).green = green;
}
int main() {
Color *arr[3];
setColor(arr, 0, 12, 23, 34);
return 0;
}
你可能想要在那些不確定的指針後面有一些實際的*對象*。 – WhozCraig 2013-03-07 20:25:57
爲什麼這麼多指針?所有那些尖刻的星星都傷害了我的眼睛。 – chris 2013-03-07 20:26:32
您爲Color *(在arr中)分配內存,但不在目標Color對象中分配內存。我很驚訝這個作品甚至有一次。 – 2013-03-07 20:26:38