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.
If you try to iterate beyond the end on an array in C, you'll get an array out of bounds error and crash. So, yeah, you basically need to use a null termination or know your bounds.
array out of bounds error
No, you won't. You'll may end up grabbing random data from the memory, and either fill your buffer with all kinds of junk left over from other code, or you'll get an access violation/segmentation fault at the end of that allocation block (depends on how your OS manages memory, if it's something like DOS or certain embedded systems, the whole memory could end up being valid).
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.
collection
Here's your problem. C doesn't have "collections". for consists of 3 fields, all of which are optional: an initialization clause, a control expression (evaluated before each execution of the body, and the for loop only executes until the expression is false), and an expression that is evaluated after each execution of the body (usually to increment or decrement a variable that is part of the control expression). Then a statement (usually a block-statement) follows.
/* i is initialized to 0 */
for (int i = 0; i < 3; i++) {
/* when i < 3 evaluates to false, execution will go to the next line following the for block */
printf("%i", i);
/* i++ is executed after each iteration*/
}
>>41
No, I am one of the people who feels the fact that you think there aren't enough programming-related threads on the front page doesn't merit shitposting, and one who disagrees with your definition of ``good thread''.
>>41
Hate to tell you this, >>26,29,32, but you're the only person who thinks you made anything resembling a point there. Might have gone better if you'd said ``char buffers'' in >>26 instead of hoping people would read your mind in a discussion about int arrays, and if you hadn't tried to bullshit about alignment. >>33 and >>34 don't have anything to be ashamed of.
>>43 Don't make me call the Xarn on you faggots, stay on fucking topic.
This thread about keeping track of the size of arrays in C. I can promise you Xarn would pity you for thinking any real discussion could be had.
>>44
I started this sub-discussion, and I have never used any language other than "array" or "buffer". If you want to restrict your solutions purely to integers for no particular reason then that makes it all the worse. Nice try though.
>>48
It's cute that you think you're the only person in the ``sub-discussion'' whose posts matter with regards to the subject at hand, you self-important windbag. Meanwhile, the adults are talking.
>>50
It's cute that you think condescension matters with regards to the subject at hand, and that your post is at all relevant and not just to make yourself feel better. Meanwhile, ``meanwhile'' isn't really a contrast to ``it's cute'' so it doesn't really make sense and is confusing if you're trying to make a point, and the people who actually know anything are talking.
>>50
You know, all I did was respond honestly to questions asked of me. Let's review: why don't I use [-1] in practice? There's lots of casting involved. Why so? Because not all of my arrays are of 32-bit elements. But because you don't like chars or shorts or doubles or structs, it's against the rules to bring up the deficiencies. Did I get that right?
To tell you the truth, I had it worked out before I was asked. The problem is that while you can use macros to alleviate cast annotations and whatnot, actually getting the length of an array is still pretty messy. It would be cleaner to use a struct. Seriously, this is just too flawed to exist in void.h.
Name:
Anonymous2010-06-15 14:58
>>55
First time poster in this thread. Could you please enlighten me why an array can only possess 256 elements in C?
>>55
You're really bad at reading comprehension, aren't you?
>>56
Because he's an idiot, and when other people are discussing arrays of ints, he expects people to read his mind and follow his train of thought when he butts in and starts attacking the straw man of char arrays (without clarifying that that's what he's doing, of course).
>>56
A char array with length stored in element[-1] doesn't have room to specify anything more. The array can be longer, but that length can't be represented.
>>57
I shouldn't need to clarify. A specialized solution when general language was used is not good enough.
>>58
All arrays are indexed by ints (or rather, the ordinal that is the size of a pointer). A char array can have just as many elements as an int array, or a long array (given enough memory, of course).
>>60
Oh, sorry, I misread >>58. (>>59 was my first post in this thread, by the way.) You are indeed correct. But why do that anyway? This isn't Pascal.
>>60
Have you considered that rather than everyone obstinately refusing to see reason and not knowing any C, you are missing the point on a now ridiculous level?
>>61
Sorry, I thought you were responsible for the broken code.
I know what you're getting at, but now that we have seen macros for allocating, deallocating and checking/setting the length, this is bulkier than using a struct. You can always ((int*)a)[-1]), which is even worse.
It's not that it doesn't work, it's just worse than the usual ways of dealing with it in C (but still better than the usual ways of doing it in Pascal.) Why do it? No reason. For the hell of it, I've already made a set of macros that work for all types and satisfies my other requirements, with the exception that it is less convenient than using even a generic struct with an int and a void payload, which itself involves a lot of needless casting. It also emits a warning under -Wall so it's pure crap.