1
int main()
{
int laiArreglo[] = {5,8,2,3,1,4,6,9,2,10}, liElemento;

printf("\nInsert the number: ");
            scanf("%d", &liElemento);
            ShowNumber(laiArreglo);
return 0;
}




void ShowNumber(int laiArreglo[])
{
    int liContador;

    printf("\nNumbers: ");

    for (liContador = 0; liContador < sizeof (laiArreglo) / sizeof (int); liContador++)
    {
        printf("%d ", laiArreglo[liContador]);
    }
}

I was using (sizeof (laiArreglo) / sizeof (int)) in main and it worked perfectly but, now inside of a fuction it doesn't work, why?.

2 Answers2

2

Keep in mind that the The name of an array "decays" to a pointer to its first element.When you use

sizeof(laiArreglo)

from main,it evaluates to

10*sizeof(int)

and not

sizeof(int*)

as it is one of the cases where decay dosen't happen.

When you use

ShowNumber(laiArreglo);

to pass it to a function, the decay does occur. So the above statement is equivalent to

ShowNumber(&laiArreglo[0]);

and when you use

sizeof(laiArreglo)

from the function ShowNumber, it evaluates to

sizeof(int*)

as laiArreglo is a pointer to int pointing to the address of the first element of the array laiArreglo.

Community
  • 1
  • 1
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

In your function ShowNumber(),what you past is a pointer rather than an array.

icecity96
  • 1,177
  • 12
  • 26