0

I am trying to pass an array to a function but the value received is only the first value in the array. What am I doing wrong ? Here are the 3 functions that are involved in the operation.

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);
}

After computeCScode, firstCode and secondCode are ok. But when passing them to invert, inside the function they only use the first value of the function. What did I forget?

1
  • Please reduce your example so that it includes only the code that is necessary to see the problem. The code you entered is way too long. (By doing that, you might find the cause of the error by yourself, too.) Commented May 14, 2012 at 20:19

1 Answer 1

1

This part of invert doesn't do what you think it does:

temp=firstCode;
firstCode=secondCode;
secondCode=temp;

If you really want to swap the contents of the arrays then use memcpy or a for loop, e.g.

for (i = 0; i < 4; ++i)
{
    int temp = firstCode[i];
    firstCode[i] = secondCode[i];
    secondCode[i] = temp;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I will change that, thank you. However, that does not solve the problem I have

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.