Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Iteration in C

Name: Anonymous 2010-06-14 3:53

Hi there /prog/.

I know Python, and I'm learning C. Both langs have for loops. Python's nicely iterates over a collection of unknown length. C's usually has you specify the length of the collection at compile time, but what if the length is unknown?

My best newbie guess: you terminate the collection with NULL, or you write an iteration function that returns some end-of-collection value when it's done.

Name: Anonymous 2010-06-14 4:20

Depends on the data structure. Usually with unknown length you're iterating through a linked list, which terminates when the pointer to the next item is null.

Name: Anonymous 2010-06-14 4:49

Null termination is one option, another is to store the length as the first element of the array.

Name: Anonymous 2010-06-14 8:38

>>7
I prefer to store it as the last element of the array, with the i!=arr[i] test.

Name: Anonymous 2010-06-14 14:03

>>11
It's almost better than using the zeroth element--which is the worst of intelligible solutions. I'd be happier using arr[-1].

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List