Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > length of strings in a two dimensional array

Reply
Thread Tools

length of strings in a two dimensional array

 
 
dr.oktopus
Guest
Posts: n/a
 
      04-19-2011
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
 
Reply With Quote
 
 
 
 
Morris Keesan
Guest
Posts: n/a
 
      04-19-2011
On Tue, 19 Apr 2011 16:54:09 -0400, 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" };
>
> 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 --
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      04-20-2011
On Tue, 19 Apr 2011 13:54:09 -0700 (PDT), "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" };
>
>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
 
Reply With Quote
 
dr.oktopus
Guest
Posts: n/a
 
      04-20-2011
>
> >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.
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      04-20-2011
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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      04-20-2011
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
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      04-20-2011
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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-20-2011
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
d
 
Reply With Quote
 
Mel Smith
Guest
Posts: n/a
 
      04-20-2011
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


 
Reply With Quote
 
dr.oktopus
Guest
Posts: n/a
 
      04-20-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM
Re: Two dimensional pointers and Two dimensional arrays Icosahedron C++ 8 08-21-2003 05:15 AM
Re: Two dimensional pointers and Two dimensional arrays John Harrison C++ 4 08-19-2003 04:00 PM
Re: Two dimensional pointers and Two dimensional arrays Alf P. Steinbach C++ 0 08-18-2003 08:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57