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


Groups > comp.lang.c > #2720

Re: length of strings in a two dimensional array

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.c
Subject Re: length of strings in a two dimensional array
Date 2011-04-20 22:26 -0400
Organization A noiseless patient Spider
Message-ID <ioo4kr$82d$1@dont-email.me> (permalink)
References <1f5669eb-6ab6-4dd7-a9e4-486134774dc0@r4g2000prm.googlegroups.com> <iomikt$mn5$1@dont-email.me> <916a0b57-ba23-490e-b90e-e99e67b85c95@z37g2000vbl.googlegroups.com>

Show all headers | View raw


On 4/20/2011 12:46 PM, dr.oktopus wrote:
> On 20 Apr, 13:12, Eric Sosman<esos...@ieee-dot-org.invalid>  wrote:
>> On 4/19/2011 4:54 PM, dr.oktopus wrote:
>>
>>> Hello,
>>> here I am talking about having good habits in code writing.
>>> I have a simple problem: have two messages that represent
>>> the same string, one in lowercase and the other in uppercase
>>> letter. Under some circustamces, I want to print the first or
>>> the second, so I couple them inside an array and use a
>>> variable "allcaps" as index.
>>> And I have to precompute the length of the strings, that are CONSTANT
>>> strings.
>>
>>> A solution is:
>>
>>> const char *messages[2] = { "this is lowercase string", "this is
>>> uppercase string" };
>>
>>       Ah. This is obviously some strange usage of the word "uppercase"
>> that I hadn't previously been aware of.
>>
>
> a better example:
>
> #define message_lo "the quick brown fox jumps over the lazy dog"
> #define message_hi "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
>
> the point is that the strings have the same length

... in which case the `?:' expression I wrote will still give the
correct answer.  The point is that if you want the strings themselves
in an array, both array elements must have the same *known* length.
That is, you can't do

	const char messages[2][] = { "hello", "HELLO" };

but must instead do

	const char messages[2][LENGTH] = ...
or
	const char messages[][LENGTH] = ...

where LENGTH is an integer constant expression.

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

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


Thread

length of strings in a two dimensional array "dr.oktopus" <blindwilly@freeonline.zzn.com> - 2011-04-19 13:54 -0700
  Re: length of strings in a two dimensional array "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-04-19 17:07 -0400
  Re: length of strings in a two dimensional array Barry Schwarz <schwarzb@dqel.com> - 2011-04-19 18:34 -0700
    Re: length of strings in a two dimensional array "dr.oktopus" <blindwilly@freeonline.zzn.com> - 2011-04-20 02:09 -0700
      Re: length of strings in a two dimensional array James Kuyper <jameskuyper@verizon.net> - 2011-04-20 07:00 -0400
        Re: length of strings in a two dimensional array Ian Collins <ian-news@hotmail.com> - 2011-04-20 23:13 +1200
          Re: length of strings in a two dimensional array James Kuyper <jameskuyper@verizon.net> - 2011-04-20 07:38 -0400
            Re: length of strings in a two dimensional array "Mel Smith" <med_cutout_syntel@aol.com> - 2011-04-20 09:25 -0600
        Re: length of strings in a two dimensional array "dr.oktopus" <blindwilly@freeonline.zzn.com> - 2011-04-20 09:51 -0700
          Re: length of strings in a two dimensional array Ben Pfaff <blp@cs.stanford.edu> - 2011-04-20 10:07 -0700
          Re: length of strings in a two dimensional array James Kuyper <jameskuyper@verizon.net> - 2011-04-20 19:57 -0400
  Re: length of strings in a two dimensional array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-20 08:12 -0400
    Re: length of strings in a two dimensional array "dr.oktopus" <blindwilly@freeonline.zzn.com> - 2011-04-20 09:46 -0700
      Re: length of strings in a two dimensional array Peter Nilsson <airia@acay.com.au> - 2011-04-20 15:11 -0700
        Re: length of strings in a two dimensional array "dr.oktopus" <blindwilly@freeonline.zzn.com> - 2011-04-21 02:26 -0700
          Re: length of strings in a two dimensional array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-21 07:44 -0400
      Re: length of strings in a two dimensional array Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-04-20 22:26 -0400

csiph-web