From: canbaby <canbaby@21cn.com>
To: "Luiz Fernando N. Capitulino" <lcapitulino@terra.com.br>
Cc: Gre Taguran <gtaguran@sni.ph>, linux-c-programming@vger.kernel.org
Subject: Re: array size 1 ? All headers
Date: Wed, 21 Jul 2004 11:19:57 +0800 [thread overview]
Message-ID: <40FDE0DD.50008@21cn.com> (raw)
In-Reply-To: <20040720235713.4cb83b20.lcapitulino@terra.com.br>
Luiz Fernando N. Capitulino wrote:
> Hi Gre,
>
>Em Wed, 21 Jul 2004 10:27:35 +0800
>"Gre Taguran" <gtaguran@sni.ph> screveu:
>
>| i think its not array of 1 but instead array of 2 which is 0 and 1 index.
>
> Wrong.
>
> K&R defines array as: array[lenght]
>
> So, if you defines an array as:
>
> int luiz[10]
>
> You will have 10 memory cells for it, and if you do:
>
> int luiz[1]
>
> You will have one memory cell.
>
> But, the _access_ is made from 0, so for the first example, we have:
>
> luiz[0], luiz[1], luiz[2]... luiz[9].
>
> For the second, just:
>
> luiz[0].
>
>| maybe
>| it is because u were thinking that the last array index (in this case is 1) is
>| null, but it is only applicable when handling string because string needs a
>| terminated string. ex.
>|
>| char tst[1];
>| tst[0]='g';
>| tst[1]='r';
>| printf("%s",tst); //this would print gr<and more chars here because it is not
>| terminated
>| printf("%c=%c"); //this will print gr meaning tst[1] is alocated by 'r'
>
> The program:
>
> char tst[1];
> tst[0]='g';
> tst[1]='r';
>
> printf("%c %c\n", tst[0], tst[1]);
>
> compiles and works here! but to be honest, I don't why. I can prove it is
>an odd case, becase this also works (here):
>
> char tst[1];
> tst[0]='g';
> tst[1]='r';
> tst[2]='z';
>
> printf("%c %c %c\n", tst[0], tst[1], tst[2]);
>
>PS: GCC compiled.
>
>
>
but your code had over the array bound, the sequent is risk.
I think the GCC should detect the index value
next prev parent reply other threads:[~2004-07-21 3:19 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-07-21 2:27 array size 1 ? All headers Gre Taguran
2004-07-21 2:57 ` Luiz Fernando N. Capitulino
2004-07-21 3:17 ` joy
2004-07-21 12:10 ` Luiz Fernando N. Capitulino
2004-07-21 3:18 ` Eric Bambach
2004-07-21 3:19 ` canbaby [this message]
2004-07-21 3:38 ` Glynn Clements
[not found] ` <200407202217.43875.eric@cisu.net>
2004-07-21 12:07 ` Luiz Fernando N. Capitulino
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=40FDE0DD.50008@21cn.com \
--to=canbaby@21cn.com \
--cc=gtaguran@sni.ph \
--cc=lcapitulino@terra.com.br \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.