Two days to fustration in #C

The first day I took some flak to be sure. Especially after mentioning getting back into C after time with Scheme. E.g. being told by three different people that I couldn’t do something, and that if I want a language that has feature X then I should use a language that has feature X. All
I was looking at was retaining some scoped state between function calls to write a generator without having to get into coroutines or breaking it into comically short files. In this particular case, adding some peculiarities to rand() rather than calling it multiple times for different ranges. Did it scant moments later with a ‘static’ declaration and hadn’t posed it as a question in the first fucking place. But like I said, I had mentioned being into Scheme. I expected some flak.

On to today, amacleod had actually suggested that iterating over an array (or in this case, a string) simply to copy its contents was “quite fast”, asking “how do you think the C libraries do it?” when I mentioned it’d be piss slow. I guess in amacleod’s world there’s only one implementation of the std C libs, and it’s a clone of the examples in K&R. For the rest of us in
the real world there’s http://bytes.com/topic/c/answers/217848-memcpy-vs-performance or http://cboard.cprogramming.com/c-programming/92339-memcpy-vs-memmove.html or any of a dozen
others all saying the same thing. Or god forbid, actually banging out the code and doing some profiling…

But hey, whatever. I fucked off and made some bacon and eggs. Nom. But when I came back it was to see /this/ bullshit in my buffer.

16:21 < amacleod> However, if you can put it in there you can’t use it as a sentinel because it’s just the number 0.
16:22 -!- para7 [~para@port-83-236-166-89.static.qsc.de] has joined ##C
16:23 -!- zommi [~andreas@nat/ibm/x-lhegmcnivhijoqxu] has quit [Read error: Connection reset by peer]
16:23 -!- arvind_khadri [~arvind@unaffiliated/arvind-khadri/x-2237230] has joined ##C
16:23 < amacleod> cc7gir, it’s really unclear to me what your code is trying to do.
16:23 < amacleod> Helsinkiii, so, tell me what “starting from the beginning” looks like in C code.
16:24 < cc7gir> amacleod: I want to find the mode of the the list of numbers
16:24 < cc7gir> the most frequently occuring number
16:24 < Helsinkiii> amacloed: probably string[i] where i=0
16:25 < Helsinkiii> i’m 2 days into c and im coming from Java, so yeah. i can’t afford our class textbook
16:25 < amacleod> Helsinkiii, yeah.. int i; char string[] = “hello”; for (i = 0; ; ) { } sounds like a good starting point.
16:25 < PoppaVic> Nice.. Well, accept the fail and off to the next class

I won’t expect the guy who lives in a strange monopoly libs world to try just answering the question (which was along the lines of copying until in a loop… which has a one line solution in the K&R book he thinks all libs are based on) but that ray of sunshine from PV was just fucking priceless.

16:26 < Helsinkiii> no
16:26 < Helsinkiii> our teacher said it’s optional
16:26 < Helsinkiii> amacleod: what i did is make 2 do whiles, nested
16:27 < Helsinkiii> the first gets the string
16:27 < Helsinkiii> the nested one copies it into an array
16:27 < Helsinkiii> then at the end, increments, and then the outer loop runs again
16:27 < amacleod> Ok.. the outer loop is because you need to get more than one string from input?
16:27 < Helsinkiii> yes
16:27 < Helsinkiii> as many as the user wants, unitl he presses enter on a blank line
16:27 < Helsinkiii> aka he enters null
16:27 -!- Xorlev [~xorlev@wl-dhcp181-207.Mines.EDU] has joined ##C
16:27 -!- Xorlev [~xorlev@wl-dhcp181-207.Mines.EDU] has quit [Changing host]
16:27 -!- Xorlev [~xorlev@unaffiliated/xorlev] has joined ##C
16:27 < amacleod> so, as nadder pointed out about half an hour ago, you can just use the address of the first element of the array as the target of fscanf
16:28 < amacleod> er.. fgets, I mean.

Which he may have, if he hadn’t been told to use a loop instead simply for the sake of disagreeing with me. But even better, now the alpha asshole steps in, who amacleod immediately gives lead to… and who, to be fair, a concesending prick though he may be, at least has the virtue of actually being familiar with C.

16:28 < Zhivago> You appear to be confused as to what “null” means.
16:28 -!- deadlock [~no_uid@unaffiliated/deadlock] has quit [Ping timeout: 272 seconds]
16:28 < cc7gir> me?
16:28 < Helsinkiii> no me
16:28 < amacleod> Heh.
16:28 < amacleod> Both a yaz, actually.
16:29 < Helsinkiii> so, how would i wrote that. whats the syntax like for doing such a feat
16:29 < amacleod> Helsinkiii was most recently speaking of the empty string (“”) as though it were “null”.
16:29 < Zhivago> I suggest reading the documentation for fgets.
16:29 < amacleod> I concur.
16:30 < Zhivago> If you cannot read then it is time to consider another profession.
16:30 < Zhivago> Perhaps landscape gardening.
16:30 < Prael> Man, I’m glad I didn’t learn C here.

That “empty string” would be a newline which is far from “empty” in C (really amacleod, leave it to Zhivago…) but other than that, ‘nuf said. If other #C’s on other networks are anything like Freenode’s, I’m hardly surprised that C’s popularity is constantly declining.

Still, it’s been serving my purposes quite admirably. One of my ‘toy interpreters’ is now a (mostly) self compiling (to C99) actors language, somewhat like an Objective C with implicit green/lightweight threading and sexps. *grins*.


February 11, 2010 - Posted by | Annoyances, Programming

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: