Wednesday, March 31, 2010

comp.lang.c - 25 new messages in 8 topics - digest

comp.lang.c
http://groups.google.com/group/comp.lang.c?hl=en

comp.lang.c@googlegroups.com

Today's topics:

* Implementing strstr - 5 messages, 4 authors
http://groups.google.com/group/comp.lang.c/t/a3fe05ab352d5774?hl=en
* Letter sent to Apress with concerns about Peter Seebach's online behavior -
6 messages, 5 authors
http://groups.google.com/group/comp.lang.c/t/482b38643777da3c?hl=en
* How comes the compiler doesn't complain about conflicting variables for the
following piece of code.. - 5 messages, 5 authors
http://groups.google.com/group/comp.lang.c/t/06d1a725e957c49a?hl=en
* difference between structure and string? - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/317fcc244d074073?hl=en
* Want / Need Genuine Google Adsense Account for Rs.300/- - 1 messages, 1
author
http://groups.google.com/group/comp.lang.c/t/b06a43cada0e040d?hl=en
* Computing a*b/c without overflow in the preprocessor - 3 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/1049c69de2e8ea27?hl=en
* 16:32 far pointers in OpenWatcom C/C++ - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c/t/4728dadef590aafe?hl=en
* Declaring an external variable as a struct - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c/t/9fba63a8c39a512b?hl=en

==============================================================================
TOPIC: Implementing strstr
http://groups.google.com/group/comp.lang.c/t/a3fe05ab352d5774?hl=en
==============================================================================

== 1 of 5 ==
Date: Wed, Mar 31 2010 10:59 am
From: Seebs


On 2010-03-31, blmblm myrealbox.com <blmblm@myrealbox.com> wrote:
> If you include purely-mathematical (i.e., non-CS) proofs, then that
> would seem to imply that no one else here has much in the way of
> mathematical training, which I strongly suspect is not true.

It seems deeply implausible to me. Both of my parents taught college
mathematics, and I retain to this day an instinctive distrust of empirical
methods as unreliable and shoddy.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


== 2 of 5 ==
Date: Wed, Mar 31 2010 11:15 am
From: Keith Thompson


blmblm@myrealbox.com <blmblm@myrealbox.com> writes:
[...]
> (It occurs to me to wonder, by the way, why we're having this
> discussion of Seebs's code in threads other than the one in which
> he posted the URL for it. But, you know -- whatever.)

It's at least partly because *you* keep discussing it with him.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


== 3 of 5 ==
Date: Wed, Mar 31 2010 11:29 am
From: Colonel Harlan Sanders


On 31 Mar 2010 16:22:52 GMT, Seebs <usenet-nospam@seebs.net> wrote:

>I would dearly love to know the *actual* reasons for which Nilges is so
>incredibly obsessive on this topic. Did he get fired from a job for owning
>a Schildt book, or for something he did which relied on information he
>derived from one of Schildt's books? Did he lose some political battle
>in which the ostensible subject was something to do with the stack, which
>seems to be a particular point of obsession? I have no idea, but I've
>always been curious. The reasons Nilges provides are consistently obviously
>fake -- the mere fact that he switches from one to another with no hiccups
>as soon as he realizes a given one won't play with a particular audience
>makes it clear that they're all just excuses and rationalizations.
>
>I'd love to know the real reason.


My guess (we'll never know the "real" reason) based on some hints in
various posts, is that he was pontificating in a programming
newsgroup about 10 years ago and Heathfield contradicted/corrected or
otherwise bruised his ego. So he set out to "take him down" As he is
now trying to do with you. At some point of this Schildt's name came
up, and Heathfield disparaged him or his book. And so Nilges took the
opposite tack..... He has been sparring with Heathfield ever since.

A year or so ago, Nilges discovers Wikipedia, and Schildt's page
therein. He decides that this is all part of the conspiracy. The
criticisms of Schildt there cite you and Feather, and his attempts to
delete this are reversed, over and over. He decides you're the
instigator of the conspiracy, and attacks you both here.....

But behind all that, he sees you as a symbol of Korporate Amerikka,
which has rejected him, despite his obvious genius, as they're all
jealous of his ability to write poetry and run marathons while coding
in assembly on his laptop. Since you get paid to program, while he can
only do it as a hobby, you remind him of his failure. So you must be
destroyed.

Well, it's a bit more plausible than Nilges' conspiracy theories, at
least.

== 4 of 5 ==
Date: Wed, Mar 31 2010 11:38 am
From: Seebs


On 2010-03-31, Colonel Harlan Sanders <Harlan@kfc.com> wrote:
> My guess (we'll never know the "real" reason) based on some hints in
> various posts, is that he was pontificating in a programming
> newsgroup about 10 years ago and Heathfield contradicted/corrected or
> otherwise bruised his ego. So he set out to "take him down" As he is
> now trying to do with you. At some point of this Schildt's name came
> up, and Heathfield disparaged him or his book. And so Nilges took the
> opposite tack..... He has been sparring with Heathfield ever since.

This is plausible. He's certainly very consistent in adopting the opposite
of anything someone he dislikes says, without any regard to whether the
resulting position is consistent with his other claims.

> Since you get paid to program, while he can
> only do it as a hobby, you remind him of his failure. So you must be
> destroyed.

This would explain his obsession with claiming that I don't program, and
then his revised obsession with claiming that I do it very badly.

Thanks. That makes a lot more sense. Also, now that I've found out that
he's not just *a* usenet kook, but *the* Usenet kook (hint: He's part
of the events from which we got "Godwin's Law"), up there in the company
of people like Archimedes Plutonium, I'm a lot more ready to accept that
his behavior is comprehensible. Loosely speaking.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


== 5 of 5 ==
Date: Wed, Mar 31 2010 2:24 pm
From: Tim Streater


In article <slrnhr72pd.aip.usenet-nospam@guild.seebs.net>,
Seebs <usenet-nospam@seebs.net> wrote:

> On 2010-03-31, Tim Streater <timstreater@waitrose.com> wrote:
> > Yes, quite possibly. What I was observing was his inability to respond
> > in a way that is germane to the issue.
>
> True.
>
> Well, consider. His actual claim was stupid. He can't win it. Since he
> can't use it to make himself look good, there's no reason for him to respond
> to it.


I know, I know. I shouldn't do it really. I picture an 18th century me,
with powdered wig and dressed like a fop, visiting Bedlam and seeing
Spinny behind bars, drooling. Very unkindly I then poke him with a
pointed stick.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689

==============================================================================
TOPIC: Letter sent to Apress with concerns about Peter Seebach's online
behavior
http://groups.google.com/group/comp.lang.c/t/482b38643777da3c?hl=en
==============================================================================

== 1 of 6 ==
Date: Wed, Mar 31 2010 11:17 am
From: Seebs


On 2010-03-31, Blash <blash1@comcast.net> wrote:
> Can't you twits figure a way to carry on your nit-picking without
> cross-posting it to groups that don't give a shit......

This isn't cross-posted, and the only more topical group for the material
is alt.usenet.kooks, which isn't as widely carried.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!


== 2 of 6 ==
Date: Wed, Mar 31 2010 11:32 am
From: Nick <3-nospam@temporary-address.org.uk>


Dr Malcolm McLean <malcolm.mclean5@btinternet.com> writes:

> On 31 Mar, 17:13, spinoza1111 <spinoza1...@yahoo.com> wrote:
>>
>> Saying that Schildt was "clear" when you believe he is wrong is
>> insincere and a lie, because clarity  is conducive to understanding,
>> understanding is knowledge, and knowledge is justified true belief.
>>
> Clarity is conducive to understanding. However you can give a very
> clear description of a false belief.

Good lord, is he still claiming that things cannot be clear and wrong?
It was that particular piece of bonkosity that caused me killfile him
yonks ago.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk


== 3 of 6 ==
Date: Wed, Mar 31 2010 11:33 am
From: Keith Thompson


Seebs <usenet-nospam@seebs.net> writes:
> On 2010-03-31, Blash <blash1@comcast.net> wrote:
>> Can't you twits figure a way to carry on your nit-picking without
>> cross-posting it to groups that don't give a shit......
>
> This isn't cross-posted, and the only more topical group for the material
> is alt.usenet.kooks, which isn't as widely carried.

The article to which Blash replied was cross-posted to comp.lang.c
and misc.invest.stocks; Blash dropped m.i.s in the followup.
The cross-posted article was by spinoza1111, but I don't think he was
the one who started the inappropriate cross-posting. As I recall,
some idiot did with the deliberate goal of bringing more trolls
into c.l.c.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


== 4 of 6 ==
Date: Wed, Mar 31 2010 11:37 am
From: Richard Harnden


On 31/03/2010 17:13, spinoza1111 wrote:
>
> [...] because clarity is conducive to understanding,
> understanding is knowledge, and knowledge is justified true belief.
>

Well, fine: somebody reading C:TCR would be justified in beliving it to
be true since its written in such a clear way.

Can you really not make the leap from 'justified true belief' to 'not
actually true at all'?


== 5 of 6 ==
Date: Wed, Mar 31 2010 2:03 pm
From: Dr Malcolm McLean


On 31 Mar, 19:37, Richard Harnden <rh.nos...@tiscali-co-uk.invalid>
wrote:
>
> Can you really not make the leap from 'justified true
> belief' to 'not actually true at all'?
>
The problem is that flase beliefs can have quite convincing
justifications.
For instance Galileo's telescope revealed that Jupiter had moons, also
that Mars was square. Theory at the time was that the heavenly bdies
were perfect, thus spheres, and orbited a centre of rotation in the
Earth.
Armed with that information, one could reasonably conclude that for
one theory to be totally wrong was plausible, but for two to be so
falsified strained credulity, and that Galieo's telescope wasn't
accurate and that the moons of Jupiter were artefacts.

== 6 of 6 ==
Date: Wed, Mar 31 2010 2:06 pm
From: Dr Malcolm McLean


On 31 Mar, 22:03, Dr Malcolm McLean <malcolm.mcle...@btinternet.com>
wrote:
> On 31 Mar, 19:37, Richard Harnden <rh.nos...@tiscali-co-uk.invalid>
> wrote:
>
> > Can you really not make the leap from 'justified true
> > belief' to 'not actually true at all'?
>
> The problem is that flase beliefs can have quite convincing
> justifications.
> For instance Galileo's telescope revealed that Jupiter had moons, also
> that Mars was square. Theory at the time was that the heavenly bdies
> were perfect, thus spheres, and orbited a centre of rotation in the
> Earth.
> Armed with that information, one could reasonably conclude that for
> one theory to be totally wrong was plausible, but for two to be so
> falsified strained credulity, and that Galieo's telescope wasn't
> accurate and that the moons of Jupiter were artefacts.

artifacts.

==============================================================================
TOPIC: How comes the compiler doesn't complain about conflicting variables for
the following piece of code..
http://groups.google.com/group/comp.lang.c/t/06d1a725e957c49a?hl=en
==============================================================================

== 1 of 5 ==
Date: Wed, Mar 31 2010 12:42 pm
From: Chad


Given the following...

#include <stdio.h>

enum values {
I = 1,
V = 5,
X = 10,
L = 50,
C = 100,
D = 500,
M = 1000
};

int main(void)
{
int D = 44;
enum values romandigits = D;
printf("The value is: %d\n", romandigits);
return 0;
}

I get....
[cdalten@localhost oakland]$ gcc -g -Wall -Wextra roman.c -o roman
[cdalten@localhost oakland]$ ./roman
The value is: 44
[cdalten@localhost oakland]$

How come the compiler doesn't complain about 'D' being both an int and
an enum?


Chad


== 2 of 5 ==
Date: Wed, Mar 31 2010 12:49 pm
From: Eric Sosman


On 3/31/2010 3:42 PM, Chad wrote:
> Given the following...
>
> #include<stdio.h>
>
> enum values {
> I = 1,
> V = 5,
> X = 10,
> L = 50,
> C = 100,
> D = 500,
> M = 1000
> };
>
> int main(void)
> {
> int D = 44;
> enum values romandigits = D;
> printf("The value is: %d\n", romandigits);
> return 0;
> }
>
> I get....
> [cdalten@localhost oakland]$ gcc -g -Wall -Wextra roman.c -o roman
> [cdalten@localhost oakland]$ ./roman
> The value is: 44
> [cdalten@localhost oakland]$
>
> How come the compiler doesn't complain about 'D' being both an int and
> an enum?

The inner declaration in the contained scope "shadows" or
"hides" the outer declaration in the containing scope.

int x = 42;
// `x' refers to the file-scope int
void f(void) {
// `x' still refers to the file-scope int
double x = 42.0;
// `x' now refers to a local double
int i;
for (i = 0; i < 10; ++i) {
// `x' still refers to the local double
char x[100];
// `x' now refers to a local array of char
...
}
// `x' refers to the local double again
...
}
// `x' refers to the file-scope int again

--
Eric Sosman
esosman@ieee-dot-org.invalid


== 3 of 5 ==
Date: Wed, Mar 31 2010 12:49 pm
From: Alexander Bartolich


Chad wrote:
> [...] How come the compiler doesn't complain about 'D' being both
> an int and an enum?

Because the declarations are in different scopes.
You do get an error if they are in the same scope.
And no, it's a feature.

--


== 4 of 5 ==
Date: Wed, Mar 31 2010 1:49 pm
From: "bartc"

"Chad" <cdalten@gmail.com> wrote in message
news:f12bba4f-44b7-4b54-84db-aed6fac6c298@z11g2000yqz.googlegroups.com...
> Given the following...
>
> #include <stdio.h>
>
> enum values {
> I = 1,
> V = 5,
> X = 10,
> L = 50,
> C = 100,
> D = 500,
> M = 1000
> };
>
> int main(void)
> {
> int D = 44;
> enum values romandigits = D;
> printf("The value is: %d\n", romandigits);
> return 0;
> }
>
> I get....
> [cdalten@localhost oakland]$ gcc -g -Wall -Wextra roman.c -o roman
> [cdalten@localhost oakland]$ ./roman
> The value is: 44
> [cdalten@localhost oakland]$
>
> How come the compiler doesn't complain about 'D' being both an int and
> an enum?

int D is in a local scope, it hides the outer D.

Try moving enum values{} inside main().

--
Bartc

== 5 of 5 ==
Date: Wed, Mar 31 2010 12:50 pm
From: "christian.bau"


On Mar 31, 8:42 pm, Chad <cdal...@gmail.com> wrote:

> How come the compiler doesn't complain about 'D' being both an int and
> an enum?

Both D's are in different scopes. The int D is enclosed in { } and
therefore D will always mean the "int D" within those braces. You
could even write

int main (void)
{
int D = 44;
{
int D = 113;
printf ("%d\n", D); // prints 113
}
printf ("%d\n", D); // prints 44
}

==============================================================================
TOPIC: difference between structure and string?
http://groups.google.com/group/comp.lang.c/t/317fcc244d074073?hl=en
==============================================================================

== 1 of 2 ==
Date: Wed, Mar 31 2010 1:15 pm
From: "Morris Keesan"


On Wed, 31 Mar 2010 06:38:47 -0400, Dr Malcolm McLean
<malcolm.mclean5@btinternet.com> wrote:

> On 31 Mar, 11:28, suaib <suaib.j...@gmail.com> wrote:
>> hhh
>>
> It's a bit of a silly question. Apart from the fact that both are
> types of data in the C programming language, structures and strings
> don't have much in common.
> It's a bit like "list the points of difference between a hawk and a
> handsaw".

Which direction is the wind?

--
Morris Keesan -- mkeesan@post.harvard.edu


== 2 of 2 ==
Date: Wed, Mar 31 2010 1:28 pm
From: Moi


On Wed, 31 Mar 2010 03:38:47 -0700, Dr Malcolm McLean wrote:

> On 31 Mar, 11:28, suaib <suaib.j...@gmail.com> wrote:
>> hhh
>>
> It's a bit of a silly question. Apart from the fact that both are types
> of data in the C programming language, structures and strings don't have
> much in common.
> It's a bit like "list the points of difference between a hawk and a
> handsaw".

What, The European handsaw, or the African handsaw ?

AvK

==============================================================================
TOPIC: Want / Need Genuine Google Adsense Account for Rs.300/-
http://groups.google.com/group/comp.lang.c/t/b06a43cada0e040d?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Mar 31 2010 1:18 pm
From: SWETHA


Genuine Google Adsense Account for Rs.300/-

We will approve your google adsense approval.

We will help you till you get the first adsense check.

Please send your mobile number to pramesh0538@gmail.com , i will call
you with in 15 minutes and give you the google adsense account.

High Price Reduction for multiple adsense accounts.

==============================================================================
TOPIC: Computing a*b/c without overflow in the preprocessor
http://groups.google.com/group/comp.lang.c/t/1049c69de2e8ea27?hl=en
==============================================================================

== 1 of 3 ==
Date: Wed, Mar 31 2010 1:41 pm
From: Francois Grieu


James Dow Allen wrote:
> On Mar 30, 1:42 pm, Francois Grieu <fgr...@gmail.com> wrote:
>> James Dow Allen wrote:
>>> [bad idea]
>> Because that's not possible in a preprocessor expression?
>>> [another bad idea]
>> That works in the preprocessor, with a fixed depth. But it generates
>> huge expressions, and won't cut it when c is coprime with a and b.
>
> I detected some of my own stupidities after clicking Send,
> but you beat me to the debunker. :-)
>
> Hoping to make amends, let me outline another solution that
> *might* work.
>
> On Mar 30, 4:52 am, Francois Grieu <fgr...@gmail.com> wrote:
>> I want to devise a C89 preprocessor macro MULDIV(a,b,c)
>> ... a, b, c, a*b/c are all in range [1..2000000000].
>> ...
>> I could live with a slightly inaccurate result,
>> provided the relative error is always at most 1/10000.
>
> You've got 31-bit inputs, but only need 14-bit precision.

Yes. I could lower precision to 1/8192 (13 bits).

> Create macro to give crude estimate of log(x); shift
> a and b right enough so that log(a) + log(b) < 32;
> do the arithmetic; shift back at the end.
>
> This might be very cumbersome (various cases depending
> on log(a)) but should avoid the other difficulties.

I fear it does not give the required precision when the result is small; in my mind 65535*65535/858967245 should give exactly 5 (either 4 or 6 is off by 20%) and 65534*65535/858967245 should give exactly 4 (either 3 or 5 is off by 25%).

> As for the preprocessor-usable log(x) estimator,
> one of two of the methods under
> "Finding integer log base 2 of an integer" at
> http://graphics.stanford.edu/~seander/bithacks.html
> might work.

I fear these expand to huge things. The least expansion that I have found so far is the straightforward (not tested)
#define LOG2(x) (30-((x)<2)-((x)<4)-((x)<8)-((x)<16)\
-((x)<32)-((x)<64)-((x)<128)-((x)<256)-((x)<512)-((x)<1024)\
-((x)<2048)-((x)<4096)-((x)<8192)-!((x)>>14)-!((x)>>15)\
-!((x)>>16)-!((x)>>17)-!((x)>>18)-!((x)>>19)-!((x)>>20)\
-!((x)>>21)-!((x)>>22)-!((x)>>23)-!((x)>>24)-!((x)>>25)\
-!((x)>>26)-!((x)>>27)-!((x)>>28)-!((x)>>29)-!((x)>>30))

This macro is needed several times, x itself can be an expression, and thus the whole thing will expand terribly.

Fran�ois Grieu


== 2 of 3 ==
Date: Wed, Mar 31 2010 1:54 pm
From: Francois Grieu


Mark Bluemel wrote :
> On 30 Mar, 09:50, Francois Grieu <fgr...@gmail.com> wrote:
>
>> I'm programming for an embedded platform, with not floats, 8-bit ALU,
>> 2kB RAM, 1 MIPS, and 0.005 s mandated boot time including 0.0035 s
>> already used. Thus pre-computing tables for timing constants is a must.
>
> So write a little program or programs to generate your constant tables
> as include files - simples.
>
> Phred is correct that the (C) preprocessor isn't the place for this.
> You could look at whether a more advanced text manipulation tool such
> as "m4" would do the job, but the natural approach is to write a
> header generating program.

Yes. I do that when I can't avoid it (such as to massage tables in preparation of faster-than-dichotomic search). But integrating that in the build/make chain is platform-dependent. On the other hand, I have loads of preprocessor-driven table constructed by the C compiler in a way that has proven perfectly portable across 3 very different embedded processors with C compilers from 3 vendors (Metrowerks, Keil, Cosmic), plus GCC & Visual Studio for test on PC. So I try to use the preprocessor when it is possible.


Francois Grieu


== 3 of 3 ==
Date: Wed, Mar 31 2010 2:07 pm
From: Keith Thompson


Francois Grieu <fgrieu@gmail.com> writes:
> James Dow Allen wrote:
[...]
>> As for the preprocessor-usable log(x) estimator,
>> one of two of the methods under
>> "Finding integer log base 2 of an integer" at
>> http://graphics.stanford.edu/~seander/bithacks.html
>> might work.
>
> I fear these expand to huge things. The least expansion that I have
> found so far is the straightforward (not tested)
> #define LOG2(x) (30-((x)<2)-((x)<4)-((x)<8)-((x)<16)\
> -((x)<32)-((x)<64)-((x)<128)-((x)<256)-((x)<512)-((x)<1024)\
> -((x)<2048)-((x)<4096)-((x)<8192)-!((x)>>14)-!((x)>>15)\
> -!((x)>>16)-!((x)>>17)-!((x)>>18)-!((x)>>19)-!((x)>>20)\
> -!((x)>>21)-!((x)>>22)-!((x)>>23)-!((x)>>24)-!((x)>>25)\
> -!((x)>>26)-!((x)>>27)-!((x)>>28)-!((x)>>29)-!((x)>>30))
>
> This macro is needed several times, x itself can be an expression,
> and thus the whole thing will expand terribly.

What's so terrible about it? The size of the expansion shouldn't make
any real difference unless it exceeds the capacity of your compiler.
Of course that's a real issue if you care about portability to
multiple compilers, some of which might have more restrictive limits.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

==============================================================================
TOPIC: 16:32 far pointers in OpenWatcom C/C++
http://groups.google.com/group/comp.lang.c/t/4728dadef590aafe?hl=en
==============================================================================

== 1 of 1 ==
Date: Wed, Mar 31 2010 1:30 pm
From: Jonathan de Boyne Pollard


>
>
>> As has been said here many times, different languages have different
>> problem domains. Most of the trouble people have is caused by using
>> the wrong language for what they're trying to do. It used to be
>> Fortran or COBOL, now, apparently, it's stuff like Java.
>
> The attraction of Java is obvious; you can hire cheap inexperienced
> unskilled individuals to crank out Java code.
>
> Typically, a fair amount of a programming class washes out when you
> get to teaching pointers and structures. Some wash out even earlier
> on recursion. These individuals were lost to employers, who had to
> spend a great deal more on skilled individuals who could understand
> such things.
>
> Java fixed that. Thanks to Java, any snot-nosed kid can write
> Important Applications.
>
> They just won't be very robust applications.
>
The fact that I've heard exactly the same argument made about Visual
BASIC, ten years ago, casts heavy doubt on the fact that it's Java that
is to blame for all of the world's ills.


==============================================================================
TOPIC: Declaring an external variable as a struct
http://groups.google.com/group/comp.lang.c/t/9fba63a8c39a512b?hl=en
==============================================================================

== 1 of 2 ==
Date: Wed, Mar 31 2010 1:58 pm
From: markhobley@hotpop.donottypethisbit.com (Mark Hobley)


Mark Hobley <markhobley@hotpop.donottypethisbit.com> wrote:
> I have created header file buffers.h as follows:
>
> struct buffertablestru {
> char *addr;
> int numbuffers;
> };
>
> extern struct buffertablestru buffertable;

I now define an external variable buffertable with a structure of
buffertablestru in buffers.c as follows:

# include "buffers.h"

struct buffertablestru buffertable = {
NULL,
0
};

This seems to be working ok now. Thanks to everyone who helped me here.
I have another query now though.

Suppose that in future, I decided to add an element to buffertablestru in
buffers.h as follows:

struct buffertablestru {
char *addr;
int futurethingy; /* <---- I have added an element */
int numbuffers;
}

The above modification would break buffers.c because I would now need to
update the definition of buffertable, because the elements are now misaligned
to the declaration.

It would be better, if I could somehow use the element names in the definition
to prevent a misalignment:

struct buffertablestru buffertable = {
addr == NULL,
numbuffers == 0
};

Does C support anything like this, maybe by providing initial values in the
header against the elements (as part of the declaration), or what is
recommended practice here, in terms of keeping declarations in a header file
in sync with the definition in the program file?

(I am using C89 if that matters.)

Mark.

--
Mark Hobley
Linux User: #370818 http://markhobley.yi.org/

== 2 of 2 ==
Date: Wed, Mar 31 2010 2:44 pm
From: "Morris Keesan"


On Wed, 31 Mar 2010 16:58:22 -0400, Mark Hobley
<markhobley@hotpop.donottypethisbit.com> wrote:

> Mark Hobley <markhobley@hotpop.donottypethisbit.com> wrote:
>> I have created header file buffers.h as follows:
>>
>> struct buffertablestru {
>> char *addr;
>> int numbuffers;
>> };
>>
>> extern struct buffertablestru buffertable;
>
> I now define an external variable buffertable with a structure of
> buffertablestru in buffers.c as follows:
>
> # include "buffers.h"
>
> struct buffertablestru buffertable = {
> NULL,
> 0
> };
>
> This seems to be working ok now. Thanks to everyone who helped me here.
> I have another query now though.
>
> Suppose that in future, I decided to add an element to buffertablestru in
> buffers.h as follows:
>
> struct buffertablestru {
> char *addr;
> int futurethingy; /* <---- I have added an element */
> int numbuffers;
> }
>
> The above modification would break buffers.c because I would now need to
> update the definition of buffertable, because the elements are now
> misaligned
> to the declaration.
>
> It would be better, if I could somehow use the element names in the
> definition
> to prevent a misalignment:
>
> struct buffertablestru buffertable = {
> addr == NULL,
> numbuffers == 0
> };
>
> Does C support anything like this, maybe by providing initial values in
> the
> header against the elements (as part of the declaration), or what is
> recommended practice here, in terms of keeping declarations in a header
> file
> in sync with the definition in the program file?
>
> (I am using C89 if that matters.)

I C89, you can't declare arbitrary initial values for structure members
while keeping the initialization safe from structure modifications.
In C99, or with a compiler which supports this feature of C99, you can
give the names of the structure members being initialized, viz:

struct buffertablestru buffertable = {
.addr = NULL;
.numbuffers = 0;
}

(see 6.7.8 of ISO/IEC 9899:1999).

The good news is that, if you don't want arbitrary initial values, but
are really going to continue initializing these values to NULL and 0, then
you don't even need to explicitly initialize this particular object,
because
objects with external storage class get initialized to the appropriate
kinds
of zero (0, NULL, 0.0, etc.) by default.

In any case, good practice would be to add new structure members only at
end of the structure, rather than in the middle, in order to avoid just the
kind of problem you're anticipating. You could even add a comment to the
structure declaration warning of problems if members are inserted other
than at the end.

--
Morris Keesan -- mkeesan@post.harvard.edu


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.c"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.c?hl=en

To unsubscribe from this group, send email to comp.lang.c+unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.c/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate