[英]Passing array to function error
我正在嘗試將數組傳遞給函數,但是接收到的值只是數組中的第一個值。 我究竟做錯了什么 ? 這是該操作涉及的3個功能。
void computeCScode (int temp1, int temp2, int code[])
{
if((temp1<100)&&(temp2<100))
{
code[0]=1;
code[1]=0;
code[2]=1;
code[3]=0;
}
else if((temp1<100)&&(temp2>=100&&temp2<=400))
{
code[0]=1;
code[1]=0;
code[2]=0;
code[3]=0;
}
...
}
void invert(int x1, int y1, int x2, int y2, int firstCode[], int secondCode[])
{
int ok=1;
int *temp;
temp=(int*)malloc(sizeof(firstCode));
int aux;
if(firstCode==0000) ok=1;
else ok=0;
...
}
void cs(HDC hdc, int x1, int y1, int x2, int y2)
{
int firstCode[4];
int secondCode[4];
FINISHED = FALSE;
DISPLAY=FALSE;
REJECTED=FALSE;
do
{
computeCScode(x1,y1,firstCode);
computeCScode(x2,y2,secondCode);
...
invert(x1,y1,x2,y2,firstCode,secondCode);
}while(!FINISHED);
}
在computeCScode之后,firstCode和secondCode正常。 但是,當將它們傳遞給反相函數時,它們僅在函數內部使用函數的第一個值。 我忘記了什么?
invert
這一部分不執行您認為的操作:
temp=firstCode;
firstCode=secondCode;
secondCode=temp;
如果您真的想交換數組的內容,請使用memcpy
或for
循環,例如
for (i = 0; i < 4; ++i)
{
int temp = firstCode[i];
firstCode[i] = secondCode[i];
secondCode[i] = temp;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.