Pregunta

in the following code:

void fusioneArray(int v[], int vL, int w[], int wL[], int *fusione)
{
   int i,j,temp;
   int k=0;
   printf("%d",wL+vL);

   for(i=0;i<vL;i++)
   {
       fusione[k]=v[i];
       k++;
   }

   for(j=0;j<wL;j++)
   {
       fusione[k]=w[j];
       k++;
   }
}

int main()
{
    int v[5]={1,2,3,4,5};
    int w[5]={5,4,3,2,1};
    int i=0;
    int fusione[10];
    fusioneArray(v,5,w,5,fusione);
}

can u explain me why vL+wL returns * instead of +? (25 instead of 10)...

¿Fue útil?

Solución

Because wL is a pointer in your code, thus you're doing pointer arithmetics instead of a standard integer arithmetics :

wL+vL = wL + vL*sizeof(int)

Since a int is 4-bytes on most platforms your wL+vL becomes 5+5*4 = 25, which is the result you get. Simply replace int wL[] with the correct int wL and you'll have the desired behavior.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top