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


Groups > comp.lang.c > #2658 > unrolled thread

length of strings in a two dimensional array

Started by"dr.oktopus" <blindwilly@freeonline.zzn.com>
First post2011-04-19 13:54 -0700
Last post2011-04-20 22:26 -0400
Articles 17 — 9 participants

Back to article view | Back to comp.lang.c


Contents

  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

#2658 — length of strings in a two dimensional array

From"dr.oktopus" <blindwilly@freeonline.zzn.com>
Date2011-04-19 13:54 -0700
Subjectlength of strings in a two dimensional array
Message-ID<1f5669eb-6ab6-4dd7-a9e4-486134774dc0@r4g2000prm.googlegroups.com>
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" };

and

#define MESSAGE_LEN (strlen(messages[0])

But what if you prefer, as good habits could suggest, to rewrite
all using only arrays and the sizeof operator?

const char message_lo[] = "this is lowercase";
const char message_up[] = "this is uppercase";

#define MESSAGE_LEN (sizeof(message_lo) - 1)

const char messages[2][MESSAGE_LEN] = { &message_lo, &message_hi };

What is better? (I think the answer is: "none")
Thanks,
            willy

[toc] | [next] | [standalone]


#2659

From"Morris Keesan" <mkeesan@post.harvard.edu>
Date2011-04-19 17:07 -0400
Message-ID<op.vt7ebijd5qv6o3@toshiba-laptop>
In reply to#2658
On Tue, 19 Apr 2011 16:54:09 -0400, dr.oktopus  
<blindwilly@freeonline.zzn.com> 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" };
>
> and
>
> #define MESSAGE_LEN (strlen(messages[0])

Besides the missing close parenthesis: defining MESSAGE_LEN this way,
you're not precomputing the length of the strings.  Every time you write
MESSAGE_LEN in your code, this will result in a call to strlen().

> But what if you prefer, as good habits could suggest, to rewrite
> all using only arrays and the sizeof operator?
>
> const char message_lo[] = "this is lowercase";
> const char message_up[] = "this is uppercase";
>
> #define MESSAGE_LEN (sizeof(message_lo) - 1)
>
> const char messages[2][MESSAGE_LEN] = { &message_lo, &message_hi };
>
> What is better? (I think the answer is: "none")

I prefer the second approach, because then MESSAGE_LEN really is a
constant, and you compute it at compile time rather than while the
program is running.  If it were me, I would also put in somewhere
a line that says
    assert(sizeof message_lo == sizeof message_up);


[Note how, in answering your message, I've included all of the context
that a reader of my message needs in order to understand what I'm saying,
rather than assuming that said reader is using Google groups and has easy
access to your message.]


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

[toc] | [prev] | [next] | [standalone]


#2667

FromBarry Schwarz <schwarzb@dqel.com>
Date2011-04-19 18:34 -0700
Message-ID<bmcsq69j3rrg74953159rbrglg7kif9iu6@4ax.com>
In reply to#2658
On Tue, 19 Apr 2011 13:54:09 -0700 (PDT), "dr.oktopus"
<blindwilly@freeonline.zzn.com> 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" };
>
>and
>
>#define MESSAGE_LEN (strlen(messages[0])
>
>But what if you prefer, as good habits could suggest, to rewrite
>all using only arrays and the sizeof operator?
>
>const char message_lo[] = "this is lowercase";
>const char message_up[] = "this is uppercase";
>
>#define MESSAGE_LEN (sizeof(message_lo) - 1)
>
>const char messages[2][MESSAGE_LEN] = { &message_lo, &message_hi };

Of course this is illegal since no part of messages contains any
address.  Did you perhaps mean to use #define instead of const char?  

And if you do, you probably need to remove the -1 from the computation
of MESSAGE_LEN.  Your stated goal is to have strings and they need the
terminating '\0'.

>
>What is better? (I think the answer is: "none")

-- 
Remove del for email

[toc] | [prev] | [next] | [standalone]


#2674

From"dr.oktopus" <blindwilly@freeonline.zzn.com>
Date2011-04-20 02:09 -0700
Message-ID<b5f8b3cf-0463-4770-85b6-91614d4037b3@cu4g2000vbb.googlegroups.com>
In reply to#2667
>
> >const char messages[2][MESSAGE_LEN] = { &message_lo, &message_hi };
>
> Of course this is illegal since no part of messages contains any
> address.  Did you perhaps mean to use #define instead of const char?  
>

Using something like:

#define message_lo "this is lowercase"
#define message_hi "this is uppercase"

prevents me to use the sizeof operator, so we return at the start
point.

> And if you do, you probably need to remove the -1 from the computation
> of MESSAGE_LEN.  Your stated goal is to have strings and they need the
> terminating '\0'.
>

No, really I don't need it. I use strings but output is not done via
printf or similar.

[toc] | [prev] | [next] | [standalone]


#2676

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-04-20 07:00 -0400
Message-ID<iomecv$8ot$1@dont-email.me>
In reply to#2674
On 04/20/2011 05:09 AM, dr.oktopus wrote:
>>
>>> const char messages[2][MESSAGE_LEN] = { &message_lo, &message_hi };
>>
>> Of course this is illegal since no part of messages contains any
>> address. �Did you perhaps mean to use #define instead of const char? �

Pay attention to the fact that this code is a constraint violation. It
doesn't matter what problems you might have with the alternative
approaches - this approach can not work, so you must choose one of the
alternatives.

> Using something like:
> 
> #define message_lo "this is lowercase"
> #define message_hi "this is uppercase"
> 
> prevents me to use the sizeof operator, so we return at the start
> point.

With the above #defines, nothing prevents you from using the expression
"sizeof message_lo"; the expression is an integer constant expression
which may be used for, among other things, the following uses:

	#define MESSAGE_LEN (sizeof message_lo - 1)
	const char messages[2][MESSAGE_LEN] =
	    {message_lo, message_high};

Note, among other things, the absense of the '&' operator from this
re-write.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#2677

FromIan Collins <ian-news@hotmail.com>
Date2011-04-20 23:13 +1200
Message-ID<917tegFl0hU3@mid.individual.net>
In reply to#2676
On 04/20/11 11:00 PM, James Kuyper wrote:
> On 04/20/2011 05:09 AM, dr.oktopus wrote:
>>>
>>>> const char messages[2][MESSAGE_LEN] = {&message_lo,&message_hi };
>>>
>>> Of course this is illegal since no part of messages contains any
>>> address. �Did you perhaps mean to use #define instead of const char? �
>
> Pay attention to the fact that this code is a constraint violation. It
> doesn't matter what problems you might have with the alternative
> approaches - this approach can not work, so you must choose one of the
> alternatives.
>
>> Using something like:
>>
>> #define message_lo "this is lowercase"
>> #define message_hi "this is uppercase"
>>
>> prevents me to use the sizeof operator, so we return at the start
>> point.
>
> With the above #defines, nothing prevents you from using the expression
> "sizeof message_lo"; the expression is an integer constant expression
> which may be used for, among other things, the following uses:
>
> 	#define MESSAGE_LEN (sizeof message_lo - 1)

Did you intend to loose the trailing '\0'?

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#2678

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-04-20 07:38 -0400
Message-ID<iomgj3$fno$1@dont-email.me>
In reply to#2677
On 04/20/2011 07:13 AM, Ian Collins wrote:
> On 04/20/11 11:00 PM, James Kuyper wrote:
>> > On 04/20/2011 05:09 AM, dr.oktopus wrote:
...
>>> >> #define message_lo "this is lowercase"
...
>> > 	#define MESSAGE_LEN (sizeof message_lo - 1)
> Did you intend to loose the trailing '\0'?

Yes, since dr. oktopus has explicitly stated that he wants to lose the
trailing '\0'. He's working with fixed-length strings, rather than
null-terminated strings (NTS). That is a legitimate choice, though not a
convenient one in C. It means he can't use any of the NTS utilities in
the C standard library.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#2691

From"Mel Smith" <med_cutout_syntel@aol.com>
Date2011-04-20 09:25 -0600
Message-ID<918c7hFac9U1@mid.individual.net>
In reply to#2678
Hi:

    I'm Mr. Butinsky here but since my language (xHarbour) is really a 
slightly higher level of C, here is the xHarbour solution:

    Function PrintUpperLower(nIndex)

        // Here we make a 'static' array which is created at compile time
        // and is only created once

        STATIC aMessages := {"this is a lowercase string","THIS IS AN 
UPPERCASE STRING"}

        // now do your printing however you can, e.g.:
        ? aMessages[nIndex]    // and the printing happens at the bnext 
place on the screen

        // or
        @ 12,10 SAY aMessages[nIndex]    // to print on line 12, column 10 
of the screen

        // or use a hardcopy printing routine available to you


    RETURN NIL


-Mel Smith

[toc] | [prev] | [next] | [standalone]


#2695

From"dr.oktopus" <blindwilly@freeonline.zzn.com>
Date2011-04-20 09:51 -0700
Message-ID<248e0773-734b-441d-b33b-de81217cdd46@z31g2000vbs.googlegroups.com>
In reply to#2676
>
> > Using something like:
>
> > #define message_lo "this is lowercase"
> > #define message_hi "this is uppercase"
>
> > prevents me to use the sizeof operator, so we return at the start
> > point.
>
> With the above #defines, nothing prevents you from using the expression
> "sizeof message_lo";

Is guaranteed that C will not apply that sizeof as if it would be
a sizeof(char *)?

[toc] | [prev] | [next] | [standalone]


#2696

FromBen Pfaff <blp@cs.stanford.edu>
Date2011-04-20 10:07 -0700
Message-ID<8762q8rgvt.fsf@blp.benpfaff.org>
In reply to#2695
"dr.oktopus" <blindwilly@freeonline.zzn.com> writes:

>> > Using something like:
>>
>> > #define message_lo "this is lowercase"
>> > #define message_hi "this is uppercase"
>>
>> > prevents me to use the sizeof operator, so we return at the start
>> > point.
>>
>> With the above #defines, nothing prevents you from using the expression
>> "sizeof message_lo";
>
> Is guaranteed that C will not apply that sizeof as if it would be
> a sizeof(char *)?

It's guaranteed.  A literal string has type "(unmodifiable) array
of char".  It is only converted to a pointer in the usual
contexts where arrays are converted to pointers.
-- 
Ben Pfaff 
http://benpfaff.org

[toc] | [prev] | [next] | [standalone]


#2719

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-04-20 19:57 -0400
Message-ID<ionrtd$tc9$1@dont-email.me>
In reply to#2695
On 04/20/2011 12:51 PM, dr.oktopus wrote:
>>
>>> Using something like:
>>
>>> #define message_lo "this is lowercase"
>>> #define message_hi "this is uppercase"
>>
>>> prevents me to use the sizeof operator, so we return at the start
>>> point.
>>
>> With the above #defines, nothing prevents you from using the expression
>> "sizeof message_lo";
> 
> Is guaranteed that C will not apply that sizeof as if it would be
> a sizeof(char *)?

Yes. A string literal in your source code is an expression with the type
"array of char". In most contexts, a expression with an array type is
automatically converted into a pointer to the first member of that
array, but there are three exceptions:

1. When the array is the operand of the sizeof operator.
2. When the array is the right operand of the unary '&' operator.
3. When the array is a string literal, used as an initializer for an array.

-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#2679

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-04-20 08:12 -0400
Message-ID<iomikt$mn5$1@dont-email.me>
In reply to#2658
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.

> and
>
> #define MESSAGE_LEN (strlen(messages[0])

     Missing a parenthesis, and not a "CONSTANT" in any event.

> But what if you prefer, as good habits could suggest, to rewrite
> all using only arrays and the sizeof operator?
>
> const char message_lo[] = "this is lowercase";
> const char message_up[] = "this is uppercase";
>
> #define MESSAGE_LEN (sizeof(message_lo) - 1)
>
> const char messages[2][MESSAGE_LEN] = {&message_lo,&message_hi };
>
> What is better? (I think the answer is: "none")

     Neither is much good, and the second won't even compile.

     It's not clear why you're so worried about CONSTANTs or about
having the strings in an array, but one possibility is

	#define LOWER "this is lowercase string"
	#define UPPER "THIS IS CAPITALIZED STRING"
	#define MESSAGE_LEN (sizeof LOWER > sizeof UPPER \
	                     ? sizeof LOWER : sizeof UPPER)
	const char messages[][MESSAGE_LEN] = { LOWER, UPPER };

I don't know whether you'll find this useful, because I don't
really understand why you've adopted all these strictures, nor
what others may lurk unposted.

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

[toc] | [prev] | [next] | [standalone]


#2694

From"dr.oktopus" <blindwilly@freeonline.zzn.com>
Date2011-04-20 09:46 -0700
Message-ID<916a0b57-ba23-490e-b90e-e99e67b85c95@z37g2000vbl.googlegroups.com>
In reply to#2679
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

[toc] | [prev] | [next] | [standalone]


#2711

FromPeter Nilsson <airia@acay.com.au>
Date2011-04-20 15:11 -0700
Message-ID<4886641d-cc58-4ad1-9ff3-347413c5d28a@d19g2000prh.googlegroups.com>
In reply to#2694
"dr.oktopus" <blindwi...@freeonline.zzn.com> wrote:
> Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> > 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,

What circumstances?

> > > 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,

Why?

> > > 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

You failed address Eric's other queries. The ones everyone else
has too. To summarise, you appear to have a problem X. You think
Y and Z are possible solutions. So you ask about Y and Z, and if
there are better ones, but fail to elaborate on what problem X
really is.

Given your limited spec, I can't see why you don't just do...

  printf("%s", allcaps
                 ? "the quick brown fox jumps over the lazy dog"
                 : "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG");

If you want to centralise strings in one source, fine, but the
solution is still rather trivial and incidental. It's not worth
spending a week pondering aesthetics. What matters more is that
your code is consistent across the whole project.

--
Peter

[toc] | [prev] | [next] | [standalone]


#2734

From"dr.oktopus" <blindwilly@freeonline.zzn.com>
Date2011-04-21 02:26 -0700
Message-ID<dfa7fc03-3b54-4973-a8c8-c4766b9809e8@k3g2000prl.googlegroups.com>
In reply to#2711
>
> Given your limited spec, I can't see why you don't just do...
>
>   printf("%s", allcaps
>                  ? "the quick brown fox jumps over the lazy dog"
>                  : "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG");
>

Because I can't. Strings are involved in a series of write() system
calls.

[toc] | [prev] | [next] | [standalone]


#2737

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-04-21 07:44 -0400
Message-ID<iop5bt$t8k$1@dont-email.me>
In reply to#2734
On 4/21/2011 5:26 AM, dr.oktopus wrote:
>>
>> Given your limited spec, I can't see why you don't just do...
>>
>>    printf("%s", allcaps
>>                   ? "the quick brown fox jumps over the lazy dog"
>>                   : "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG");

     Ah.  This is obviously some strange and unusual usage of the
identifier `allcaps' that I hadn't previously been aware of.

> Because I can't. Strings are involved in a series of write() system
> calls.

     write(fd,
           allcaps ? "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
                   : "the quick brown fox jumps over the lazy dog",
           sizeof    "The mixed-case Fox jumped over the lazy Dog" - 1);

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

[toc] | [prev] | [next] | [standalone]


#2720

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-04-20 22:26 -0400
Message-ID<ioo4kr$82d$1@dont-email.me>
In reply to#2694
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web