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-15 2:21

>>39
Let me guess, you are one of >>33,34 and ashamed?

Name: Anonymous 2010-06-15 2:24

>>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''.

Name: Anonymous 2010-06-15 2:30

>>35-42
Don't make me call the Xarn on you faggots, stay on fucking topic.
>>1
You can get the size of an array using sizeof.

Name: Anonymous 2010-06-15 2:32

>>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.

Name: Anonymous 2010-06-15 2:35

>>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.

Name: Anonymous 2010-06-15 4:08

BAMPV XARNÆ

Name: Applesauce 2010-06-15 5:30

>>1
while(condition)

Name: Anonymous 2010-06-15 8:54

>>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.

Name: Anonymous 2010-06-15 9:55

HAX MY ANUS

Name: Anonymous 2010-06-15 10:10

>>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.

Name: Anonymous 2010-06-15 10:17

>>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.

Name: Anonymous 2010-06-15 10:41

>>51
fail. GTFO

Name: Anonymous 2010-06-15 11:38

C is not an OO languange, hence there are no collections or iterators. We use pointers and hard coded array lengths you STUPID FUCK.

Name: Anonymous 2010-06-15 11:56

>>53
LoL

Name: >>48 2010-06-15 14:46

>>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: Anonymous 2010-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?

Name: Anonymous 2010-06-15 15:13

>>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).

Name: Anonymous 2010-06-15 15:53

>>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.

Name: Anonymous 2010-06-15 16:00

>>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).

Name: Anonymous 2010-06-15 18:01

>>59
Indexing really doesn't have anything to do with the issue. I see you still haven't tried that code with char.

Name: Anonymous 2010-06-15 18:30

>>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.

But still, try this.
char *array = malloc (size + 4)
array += 4
#define LEN(a) (*(int*)(a)-4)

Name: Anonymous 2010-06-15 19:22

>>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?

Name: Anonymous 2010-06-15 20:09

>>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.

Name: Anonymous 2011-02-03 5:23

Name: Anonymous 2011-12-08 20:40

he gives him the alley-oop

Name: Anonymous 2011-12-08 20:41

LE DOOBS!!! >:D INSPECT 'EM!!!!!

Name: Anonymous 2011-12-08 20:57

#include <ooc/lang/Iterable.h>

Name: Anonymous 2011-12-08 21:25

>>67

https://github.com/nddrylliog/rock/blob/master/sdk/lang/Iterators.ooc

is this what the ooc poster is always cryptically referencing

Name: Anonymous 2011-12-08 21:47

>>68

each: func ~withIndex (f: Func (T, Int)) {
        index := 0
        for(elem in this) {
            f(elem, index)
            index += 1
        }
    }

rock is an ooc compiler that generates C99.



This is like my dream language, syntax that looks nice and it seems you can mix in C code with it

Name: Anonymous 2011-12-09 0:04

>>69
seems you can mix in C code with it
is this C++?

Name: Anonymous 2011-12-09 0:48

>>69
I don't like it, it seems to have static typing

Name: Anonymous 2011-12-09 5:22

>>71

wow people actually prefer dynamic typing? is this some kind of sick joke?

Name: Anonymous 2011-12-09 5:27

Use a for each loop

Name: Anonymous 2011-12-09 7:22

>>72
I actually do.

Name: Anonymous 2011-12-09 9:06

>>72
Wow, people don't prefer dynamic typing? Is this some kind of sick joke?

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