Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #17614

Re: Arrays Vs Pointers

From Markus Wichmann <nullplan@gmx.net>
Newsgroups comp.lang.c
Subject Re: Arrays Vs Pointers
Date 2012-02-19 18:54 +0100
Organization 1&1 Internet AG
Message-ID <rjp719-cl5.ln1@voyager.wichi.de.vu> (permalink)
References <jgs51h$hhm$1@speranza.aioe.org> <00b8a482-7d91-4c87-91b5-1b1f36446c0c@db5g2000vbb.googlegroups.com> <5je0k7t3165kv987s7jj66mud50kj18rae@4ax.com> <20120218214643.209@kylheku.com>

Show all headers | View raw


On 19.02.2012 06:53, Kaz Kylheku wrote:
> If we could throw away sequence points and have left-to-right evaluation,
> we could simply write:
> 
>   length = 256 * *pkt_ptr++ + *pkt_ptr++;
> 

But we don't, so we can't. I don't get what your difficulties are with
that. C is the way it is, Java is different. In case you didn't notice,
Java is built on different principles: C's directive is "trust the
programmer" (though the inclusion of complex and imaginary numbers
somewhat goes against this) and Java's directive is "protect the
programmer from his own mistakes".

Where Java falls short is that the really bad mistakes programmers make
aren't the kind a compiler could find. But OK, there are nearly-as-bad
mistakes that Java takes care of nicely (memory-leaks, free-after-use,
access violations with corruption, ...). Only sometimes it is too
obsessive with that. Like, it doesn't let you use variables that may be
uninitialized. Which would be a lot more helpful if the compiler
actually made a proper data-flow analysis instead of the half-assed
stuff it's doing currently. Try the following test class:

public class Test {
    public static void main(String[] args)
    {
        int i;

        if (args.length > 3)
            i = 3;
        if (args.length <= 3)
            i = 2;
        System.out.println(i);
    }
}

and compare the output of the compiler with what you get from a decent C
compiler at highest warning level when fed this source code:

#include <stdio.h>

int main(int argc, char* argv[])
{
    int i;

    (void) argv;
    if (argc > 3)
        i = 3;
    if (argc <= 3)
        i = 2;

    printf("%d\n", i);
    return 0;
}

And I don't get what is so hard about

length = (pkt_ptr[0] << 8) + pkt_ptr[1];
pkt_ptr += 2;

either.

> Thus:
> 
>>> Each time you read a byte you advance the pointer you don't have to
>>> keep track and count bytes yourself. So is the CRC field in byte 21 or
>>> byte 22? Well it depends if the Qualifier option was selected.
> 
> Exactly.
> 
>> That works with an index variable also.
>>   int i = 0; // or unsigned or size_t
>>   type = pkt[i++];
>>   len = pkt[i] * 256 + pkt[i+1]; i += 2;
>> or other variants similarly.
> 
> When I write this kind of code, I have the sense that I'm being the "human
> compiler".
> 
> In my head I wrote this:
> 
>   length = 256 * *pkt_ptr++ + *pkt_ptr++;
> 

If you want to write a line like this, you can! Just don't feed it a C
compiler, but Java, if that's what you want.

Otherwise, why don't you just build a C compiler with that rule
implemented? The behaviour is undefined by C which means that your
compiler would still be conforming.

> But the nice, well-ordered language in my well-ordered mind has to be compiled
> by hand into the disordered language in the machine, e.g.:
> 
>   length = 256 * *pkt_ptr++;
>   length += *pkt_ptr++;
> 

You really don't know what compiling by hand is. If you were ever in a
situation where operator overloading of C++ truly was helpful, then
you'll know if you had to rewrite that stuff into C. (Yeah, I think the line

y = 2 * x + 15 * a + (b - c) / d;

was better than the block

{
    Rational t1, t2, t3;
    rat_set_i(&t1, 2, 1);
    rat_mul(&t1, &t1, &x);
    rat_set_i(&t2, 15, 1);
    rat_mul(&t2, &t2, &a);
    rat_add(&t1, &t1, &t2);
    rat_sub(&t3, &b, &c);
    rat_div(&t3, &t3, &d);
    rat_add(&y, &t1, &t3);
}

)

Ciao,
Markus

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Arrays Vs Pointers peter <pc722@nospam.mail.com.br> - 2012-02-07 21:28 +0000
  Re: Arrays Vs Pointers William Ahern <william@wilbur.25thandClement.com> - 2012-02-07 13:50 -0800
  Re: Arrays Vs Pointers James Kuyper <jameskuyper@verizon.net> - 2012-02-07 17:13 -0500
  Re: Arrays Vs Pointers Stephen Sprunk <stephen@sprunk.org> - 2012-02-07 16:38 -0600
    Re: Arrays Vs Pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-07 23:09 +0000
  Re: Arrays Vs Pointers Keith Thompson <kst-u@mib.org> - 2012-02-07 14:41 -0800
    Re: Arrays Vs Pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-07 23:07 +0000
      Re: Arrays Vs Pointers James Kuyper <jameskuyper@verizon.net> - 2012-02-07 19:14 -0500
    Re: Arrays Vs Pointers Shao Miller <sha0.miller@gmail.com> - 2012-02-07 18:21 -0500
      Re: Arrays Vs Pointers Keith Thompson <kst-u@mib.org> - 2012-02-07 16:25 -0800
        Re: Arrays Vs Pointers Shao Miller <sha0.miller@gmail.com> - 2012-02-07 20:31 -0500
    Re: Arrays Vs Pointers Ike Naar <ike@sverige.freeshell.org> - 2012-02-07 23:36 +0000
      Re: Arrays Vs Pointers Keith Thompson <kst-u@mib.org> - 2012-02-07 16:26 -0800
    Re: Arrays Vs Pointers Keith Thompson <kst-u@mib.org> - 2012-02-07 16:24 -0800
  Re: Arrays Vs Pointers Paul N <gw7rib@aol.com> - 2012-02-07 14:55 -0800
  Re: Arrays Vs Pointers Shao Miller <sha0.miller@gmail.com> - 2012-02-07 18:11 -0500
  Re: Arrays Vs Pointers John Bode <jfbode1029@gmail.com> - 2012-02-08 16:06 -0800
    Re: Arrays Vs Pointers Philip Lantz <prl@canterey.us> - 2012-02-08 23:25 -0800
      Re: Arrays Vs Pointers John Bode <jfbode1029@gmail.com> - 2012-02-09 05:34 -0800
        Re: Arrays Vs Pointers Philip Lantz <prl@canterey.us> - 2012-02-10 01:04 -0800
  Re: Arrays Vs Pointers Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-09 00:20 -0800
    Re: Arrays Vs Pointers 88888 Dihedral <dihedral88888@googlemail.com> - 2012-02-09 23:42 -0800
    Re: Arrays Vs Pointers gwowen <gwowen@gmail.com> - 2012-02-10 01:20 -0800
    Re: Arrays Vs Pointers David Thompson <dave.thompson2@verizon.net> - 2012-02-18 23:12 -0500
      Re: Arrays Vs Pointers Kaz Kylheku <kaz@kylheku.com> - 2012-02-19 05:53 +0000
        Re: Arrays Vs Pointers Markus Wichmann <nullplan@gmx.net> - 2012-02-19 18:54 +0100
          Re: Arrays Vs Pointers Willem <willem@toad.stack.nl> - 2012-02-19 18:51 +0000
          Re: Arrays Vs Pointers "io_x" <a@b.c.invalid> - 2012-02-19 21:29 +0100
          Re: Arrays Vs Pointers Markus Wichmann <nullplan@gmx.net> - 2012-02-20 11:24 +0100
          Re: Arrays Vs Pointers gwowen <gwowen@gmail.com> - 2012-02-21 01:51 -0800
  Re: Arrays Vs Pointers Willem <willem@toad.stack.nl> - 2012-02-19 12:32 +0000
    Re: Arrays Vs Pointers Keith Thompson <kst-u@mib.org> - 2012-02-19 12:21 -0800
      Re: Arrays Vs Pointers Willem <willem@toad.stack.nl> - 2012-02-19 21:00 +0000
        Re: Arrays Vs Pointers Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-02-19 21:42 +0000
      Re: Arrays Vs Pointers pete <pfiland@mindspring.com> - 2012-02-20 17:32 -0500
        Re: Arrays Vs Pointers James Kuyper <jameskuyper@verizon.net> - 2012-02-20 17:56 -0500
        Re: Arrays Vs Pointers Keith Thompson <kst-u@mib.org> - 2012-02-20 15:19 -0800
          Re: Arrays Vs Pointers Keith Thompson <kst-u@mib.org> - 2012-02-20 16:35 -0800
            Re: Arrays Vs Pointers pete <pfiland@mindspring.com> - 2012-02-20 22:53 -0500
              Re: Arrays Vs Pointers Keith Thompson <kst-u@mib.org> - 2012-02-20 21:16 -0800
            Re: Arrays Vs Pointers Richard Sanders <lnr729@gmail.com> - 2012-02-21 00:26 -0800
              Re: Arrays Vs Pointers pete <pfiland@mindspring.com> - 2012-02-21 11:46 -0500

csiph-web