#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int width = 100;
int height = 100;
float cam[] = {-1.1,-1.0,1.2};
float D[] = {0.2,0.2,-0.2};
float U[] = {0.0,1.0,0.0};
float* cross(float* v1,float* v2)
{
float el1[] = {(v1[1]*v2[2]-v1[2]*v2[1]),(v1[2]*v2[0]-v1[0]*v2[2]),(v1[0]*v2[1]-v1[1]*v2[0])};
return el1;
}
float* neg3(float* v)
{
float arr[3];
for(int i=0;i<3;i++)
arr[i] = 0.0-(v[i]);
return arr;
}
/*
float* cam_space(float* p)
{
float* e1 = cross(D,U);
float* e2 = cross(D,cross(U,D));
float* e3_n = D;
float ar[4];
ar[0] = e1[0]*p[0]+e1[1]*p[1]+e1[2]*p[2];
ar[1] = e2[0]*p[0]+e2[1]*p[1]+e2[2]*p[2];
ar[2] = -(e3_n[0]*p[0]+e3_n[1]*p[1]+e3_n[2]*p[2]);
ar[3] = p[3];
return ar;
}
*/
float* translate(float* p,float* v)
{
float arr1[3];
for(int i=0;i<=2;i++)
arr1[i] = p[i] + v[i];
return arr1;
}
int main()
{
float* poi;
poi = cam; //undo later
float* nc;
nc = neg3(cam);
cout<<" "<<nc[0]<<" "<<nc[1]<<" "<<nc[2]<<endl;
float arbit[3] = {0.1,0.1,0.1};
float* temp1;
temp1 = translate(poi,arbit);
//float* temp2;
//temp2 = cam_space(temp);
cout<<" "<<nc[0]<<" "<<nc[1]<<" "<<nc[2]<<endl;
cout<<" "<<poi[0]<<" "<<poi[1]<<" "<<poi[2]<<endl;
cout<<" "<<temp1[0]<<" "<<temp1[1]<<" "<<temp1[2]<<endl;
return 0;
}
正如你所看到的,我輸出了兩次nc
。但是這兩個值不同。第二次顯示nc
,它實際上顯示的值爲temp1
,而temp1
實際上顯示的是垃圾值。 有什麼幫助嗎?C++指針與數組
您沒有使用STDLIB或標準輸入輸出功能,從而擺脫了頭。同樣在未來您可能想使用cstdlib和cstdio來代替,因爲這裏提到的推理http://stackoverflow.com/questions/2847729/whats-the-main-difference-between-stdlib-h-and-cstdlib-in- 2847753#2847753 此外,如果這些全局變量是全球常數 - 將它們標記爲這樣。 –
其他人都說過(所有人都+1)以及[陣列是邪惡的](http://www.parashift.com/c++-faq-lite/containers.html)。 –