Re: sizeof and passing pointer
>I'm a beginning C programmer and I have a question regarding arrays and
>finding the number of entries present within an array.
>
>If I pass an array of structures to a function, then suddenly I can't use
>sizeof(array) / sizeof(array[0]) anymore within that function ?
You can pass a *pointer* to an array of structures to a function.
It may look like you're passing the array, and the syntax makes it
easy to be fooled, but you're not. The size of the array you passed
isn't passed with the pointer to the array, so if you want the size,
you have to do it manually (e.g. pass it as another argument to the
function).
> - What point am I missing ?
sizeof applied to a pointer does not give the size of what it points
at, except occasionally by accident. It gives the size of the pointer.
|