Sam Couter wrote:

O Plameras <[EMAIL PROTECTED]> wrote:
In doing so I have a dramatic change in the way my program now behaves.

No. A for loop is just a different way of expressing a while loop;
they're different syntax but identical in behaviour. Watch:

Yes I have changed the behavior of the original C program, if you consider this,

If I changed this:
From, int integer_array[] = {1,-2,3,-4,5,-6,-7,8,-9,32727000}; To, int integer_array[] = {1,-2,3};

I must also change this,
From, for (i = 0; i < 10; i++) {...}
To, for (i = 0; i < 3; i++) {...}

to make the program work.

In my modified program, if I changed this,
From, int integer_array[] = {1,-2,3,-4,5,-6,-7,8,-9,32727000}; To, int integer_array[] = {1,-2,3};

Don't have to change this,
past_end = integer_array + sizeof(integer_array)/sizeof(int);
while (ptr < past_end) {...}

to make the program work.

If this is not a change in behavior, what is ?

What I'm also trying to point out is that C is better with
pointer-arithmetic which many languages do not have including
O'Caml.

for (initialise; guard; increment) { body }

initialise; while (guard) { body; increment; }


Any programmer or computer person ( experienced or novice) knows this.
Using a for loop or while loop, you've still explicity stated the
initial state (start of the array), the guard condition (not at the end
of the array yet) and the increment (next position in the array).

Yes, that's true I had to in my code. The point is I don't have to change my while-loop each time I change my array size in my specific example.
In the modified program  the size of array may be modified at will and
there is no need to remembering and change the loop.  We may format
our array anywhere in the application and no need to change the loop.

Only works for static sized arrays, as has been demonstrated, and leads
to insidious hard to detect bugs when you change to dynamic arrays
because they're just pointers.

That's what I have in my program.  I have specified a static array.

Show us a specific situation where  in that code  the modified program
will not work.
This is where the power of C-pointer lies. If I am prepared and able,
as a programmer, to put good and compact pointer-arithmetic into my
programs  I'd  get  better and concise C-programs. It is in the ability
of the C-programmer. He has a fine-grained control as to what should
happen.

Humans make mistakes. Minimising the impact of the mistakes is a winning
strategy. Ignoring the fact that people make mistakes is a losing
strategy. Use C when you must, something smarter when you can.

Aha !  So, it's C language.

Good thing about C is that it has one of the least number of vocabularies to learn and master.
Erik said:

By contrast, the O'caml mapi function automatically figures out
the start and end conditions from what it knows about the array.
This is what the modified C-program does with the concised while loop.

No it doesn't. You explicity told it the initial state and guard
condition, and also the increment operation. Any language with a foreach
construct will actually figure those out itself. C will not.
I've explained this previously.
--
O Plameras
http://www.acay.com.au/~oscarp/tutor

--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to