0
我有這段代碼,但是我不確定如何在通過引用將 結構傳遞給函數後訪問結構指針,程序在此行崩潰, 訪問指針不工作。通過引用函數傳遞結構後訪問結構指針
scanf("(%lf,%lf)",polygon->xvals[i],polygon->yvals[i]);
固定碼,感謝所有誰回答
struct Polygon{
double *xvals, *yvals;
int numverts;
};
typedef struct Polygon pol;
pol getpoly(pol *polygon);
int main(){
pol polygon;
getpoly(&polygon);
}
pol getpoly(pol *polygon){
polygon->xvals = (double *)malloc(sizeof(double)*polygon->numverts);
polygon->yvals = (double *)malloc(sizeof(double)*polygon->numverts);
check=0;
int i;
for(i=0;i<10;i++){
while(check !=2){
cout<<"enter vertices "<<i<<" (x,y)\n";
check = scanf("(%lf,%lf)",&polygon->xvals[i],&polygon->yvals[i]);
_flushall();
}
check=0;
}
polygon->xvals[polygon->numverts-1] = polygon->xvals[0];
polygon->yvals[polygon->numverts-1] = polygon->yvals[0];
return *polygon;
}
道歉,我沒有解釋實際發生了什麼問題,程序失敗,我試圖將東西放入指針 –
我已經編輯了標籤。 C. –
@ n.m中沒有提及。代碼中也沒有引用;它對我來說看起來像是有效的C(除了當然在'main'中缺少'return')。 –