On Tue, Sep 27, 2005 at 11:46:20PM +1000, O Plameras wrote:
> Erik de Castro Lopo wrote:
>
> >You will notice that something like the Array.mapi function is
> >much less likely to contain errors than the C for loop.
> >
> >
> I can modify my C-program to remove that problem in the ff. So,
> as to whether a C-program is more prone to error relies on the
> manner and style of coding and not intrinsic to C-language. Don't
> you think ?
It's still brittle; for instance look what happens if you
naively try to move printing to a function:
#include <stdio.h>
void p(int integer_array[])
{
int *ptr, *past_end, *iptr;
ptr = &integer_array[0];
iptr = ptr; printf("\n\n");
past_end = integer_array + sizeof(integer_array)/sizeof(int);
while (ptr < past_end)
{
printf("integer_array[%d] = %d ",(ptr - iptr),*ptr);
ptr++;
putchar('\n');
}
}
int main(void)
{
int some_integer_array[] = {1,-2,3,-4,5,-6,-7,8,-9,32727000};
p(some_integer_array);
}
$ make op-array
cc op-array.c -o op-array
$ ./op-array
integer_array[0] = 1
$
OOPS! -- sizeof gives the size of int*, not int[].
I'd give the C++ equivalant, but without static initializers for vectors,
(coming in the next standard!) it's a little too ugly :-)
Matt
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html