473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Yet another nitpicky style question

Do you prefer *my_string or my_string[0]? Why?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
17 1089

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c0**********@chessie.cirr.com...
Do you prefer *my_string or my_string[0]? Why?


depends. somestruct->charptr[0] is easier to read than
*(somestruct->charptr) I think...

But

*charptr++ = 'a';

Is cleaner than

charptr[0] = 'a'; ++charptr;

Tom
Nov 14 '05 #2

"Tom St Denis" <to*@securescience.net> wrote in message
news:qZ**************@news01.bloor.is.net.cable.ro gers.com...

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c0**********@chessie.cirr.com...
Do you prefer *my_string or my_string[0]? Why?


depends. somestruct->charptr[0] is easier to read than
*(somestruct->charptr) I think...


Note: [before anyone gets wise] I normally put () around my member
references just to make the intent clear.

Tom
Nov 14 '05 #3
Tom St Denis <to*@securescience.net> wrote:
"Tom St Denis" <to*@securescience.net> wrote in message
news:qZ**************@news01.bloor.is.net.cable.ro gers.com...
depends. somestruct->charptr[0] is easier to read than
*(somestruct->charptr) I think...

Note: [before anyone gets wise] I normally put () around my member
references just to make the intent clear.


I'm assuming then that the '->' binds more tightly than the '*'?

--
Harrison Caudill | .^ www.hypersphere.org
Computer Science & Physics Double Major | | Me*Me=1
Georgia Institute of Technology | v' I'm just a normal guy
Nov 14 '05 #4
Charles Harrison Caudill <ku*****@myrna.cc.gatech.edu> spoke thus:
I'm assuming then that the '->' binds more tightly than the '*'?


Quite so.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5

"Charles Harrison Caudill" <ku*****@myrna.cc.gatech.edu> wrote in message
news:c0**********@solaria.cc.gatech.edu...
Tom St Denis <to*@securescience.net> wrote:
"Tom St Denis" <to*@securescience.net> wrote in message
news:qZ**************@news01.bloor.is.net.cable.ro gers.com...

depends. somestruct->charptr[0] is easier to read than
*(somestruct->charptr) I think...

Note: [before anyone gets wise] I normally put () around my member
references just to make the intent clear.


I'm assuming then that the '->' binds more tightly than the '*'?


Yes. Just

*somestruct->charptr

is harder for me to read than

*(somestruct->charptr)

Even though they mean the same thing [the actual "wrong" code alluded to
would be (*somestruct)->charptr which means something else].

Tom
Nov 14 '05 #6
On Fri, 13 Feb 2004 13:03:42 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Do you prefer *my_string or my_string[0]? Why?


First off, I'm wondering whether you're asking about the limited case
of fetching just the first character of an array, or the more general
question "when is it better to traverse an array with a pointer vs.
using subscripts"? But for the most part, I think both questions share
the same answer [although the latter general question invites
performance-related arguments, most of which probably wouldn't stand
up to close scrutiny in this group ;-) ]

I'd ask myself whether one way or the other better reflects the task
at hand ("better" in this case having a high probability of being
decided on a subjective basis) and/or contributes to a more consistent
approach. Let's see, an example would be good...

#include <ctype.h>

int startsWithId(char const str[])
{
return isupper(str[0]) || str[0] == '_'; // consistent
// or (pick ONE):
return isalpha(*str) || *str == '_'; // not consistent?
}

So first I'd decide whether I wanted to represent the parameter as an
array or as a pointer, and then I'd be consistent in the body of the
function. Or, the process may run in reverse: I'll first find the more
natural notation for the implementation, and then make sure the
declaration is consistent with /that/.

In the example above, I really don't have a preference between
declaring str as an array or as a pointer, but whichever I picked, I'd
just try to be consistent.
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #7
int startsWithId(char const str[])
{
return isupper(str[0]) || str[0] == '_'; // consistent
// or (pick ONE):
return isalpha(*str) || *str == '_'; // not consistent?
}


Darn it, that second one was supposed to be "isupper" also. Now why
couldn't the compiler have caught that for me? Compilers and
spell-checkers...
Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #8

int startsWithId(char const str[])
{
return isupper(str[0]) || str[0] == '_'; // consistent
// or (pick ONE):
return isalpha(*str) || *str == '_'; // not consistent?
}


I'm /not/ having a good day. The /first/ one is supposed to be
"isalpha". Grrr. (That's what I get for switching my example in
mid-stride.)

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #9
On Fri, 13 Feb 2004 13:03:42 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Do you prefer *my_string or my_string[0]? Why?


Depends. Which one expresses your intent better, in the context it's
used in?

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #10
In <c0**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Do you prefer *my_string or my_string[0]? Why?


It depends on the context. If I need it to access only the character
at that address, it's going to be pointer dereferencing syntax, if
I need to access more characters, at various offsets, it will be the
array access syntax.

Examples:

if (*p == 'a') ...

if (p[0] == 'a' && p[1] == 'b') ...

but never the equivalent:

if (*p == 'a' && p[1] == 'b') ...

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
"Leor Zolman" <le**@bdsoft.com> wrote in message
news:rc********************************@4ax.com...

int startsWithId(char const str[])
{
return isupper(str[0]) || str[0] == '_'; // consistent
// or (pick ONE):
return isalpha(*str) || *str == '_'; // not consistent?
}


I'm /not/ having a good day. The /first/ one is supposed to be
"isalpha". Grrr. (That's what I get for switching my example in
mid-stride.)


The argument to either isxxxx call should be cast to unsigned char.

--
Peter
Nov 14 '05 #12
Peter Nilsson wrote:
"Leor Zolman" <le**@bdsoft.com> wrote in message
news:rc********************************@4ax.com...
>
>int startsWithId(char const str[])
>{
> return isupper(str[0]) || str[0] == '_'; // consistent
> // or (pick ONE):
> return isalpha(*str) || *str == '_'; // not consistent?
>}


I'm /not/ having a good day. The /first/ one is supposed to be
"isalpha". Grrr. (That's what I get for switching my example in
mid-stride.)


The argument to either isxxxx call should be cast to unsigned char.


Why? (That is, I know why you're saying it, but have you thought about the
consequences?)

I think it would be better to check whether the value you're going to pass
is /changed/ if you convert it to unsigned char. If so, then you're not
really testing what you think you're testing. And if not, why cast?

Thus:

if(ch == (unsigned char)ch)
{
Might as well just call isalpha(ch);
}
else
{
Well, isalpha would only return 0 anyway, and you've learned something
important about ch.
}

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #13
"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message
news:c0**********@hercules.btinternet.com...
Peter Nilsson wrote:
"Leor Zolman" <le**@bdsoft.com> wrote in message
news:rc********************************@4ax.com...

>
>int startsWithId(char const str[])
>{
> return isupper(str[0]) || str[0] == '_'; // consistent
> // or (pick ONE):
> return isalpha(*str) || *str == '_'; // not consistent?
>}

I'm /not/ having a good day. The /first/ one is supposed to be
"isalpha". Grrr. (That's what I get for switching my example in
mid-stride.)
The argument to either isxxxx call should be cast to unsigned char.


Why? (That is, I know why you're saying it, but have you thought about the
consequences?)

I think it would be better to check whether the value you're going to pass
is /changed/ if you convert it to unsigned char.


I don't think it's unreasonable to assume that negative characters are
changed _back_ to their original value [in all but a handful of exceptional
cases, e.g. 1c or sm machines where -0 may convert 'back' to 0 rather than
255 or 128 say.]

Given a choice between potential undefined behaviour and well defined
behaviour (on 'sane' implemetations), I choose the latter. [Presumably your
book says the same?] Anyway...
If so, then you're not
really testing what you think you're testing. And if not, why cast?

Thus:

if(ch == (unsigned char)ch)
{
Might as well just call isalpha(ch);
}
else
{
Well, isalpha would only return 0 anyway,
Huh?
and you've learned something
important about ch.
}


AFAICS, the program won't have learnt anything at all from the test. What
stops a character from being (originally sourced as) an alpha character,
having a negative value in a plain char representation in non "C" locales?

If Leor wanted purely to test for characters in the set A-Za-z, then the
only sure way (from a strict conformance POV) is to explicity compare
against that character set.

What am I missing?

--
Peter
Nov 14 '05 #14
Peter Nilsson wrote:
"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message
news:c0**********@hercules.btinternet.com...
Peter Nilsson wrote:
>
> The argument to either isxxxx call should be cast to unsigned char.


Why? (That is, I know why you're saying it, but have you thought about
the consequences?)

I think it would be better to check whether the value you're going to
pass is /changed/ if you convert it to unsigned char.


I don't think it's unreasonable to assume that negative characters are
changed _back_ to their original value [in all but a handful of
exceptional cases, e.g. 1c or sm machines where -0 may convert 'back' to 0
rather than 255 or 128 say.]


Changed back /when/? Not in isalpha(), that's for sure. Consider this code
(in a char == signed environment):

char c = somenegativevalue;
unsigned char uc = c;
int i = uc;
printf("%d\n", i);

This is going to print a non-negative value. i == c is bound to compare
false.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #15
Richard Heathfield <do******@address.co.uk.invalid> wrote in message news:<c0**********@sparta.btinternet.com>...
Peter Nilsson wrote:
"Richard Heathfield" <do******@address.co.uk.invalid> wrote in message
news:c0**********@hercules.btinternet.com...
Peter Nilsson wrote:
>
> The argument to either isxxxx call should be cast to unsigned char.

Why? (That is, I know why you're saying it, but have you thought about
the consequences?)

I think it would be better to check whether the value you're going to
pass is /changed/ if you convert it to unsigned char.


I don't think it's unreasonable to assume that negative characters are
changed _back_ to their original value [in all but a handful of
exceptional cases, e.g. 1c or sm machines where -0 may convert 'back' to 0
rather than 255 or 128 say.]


Changed back /when/? Not in isalpha(), that's for sure. Consider this code
(in a char == signed environment):

char c = somenegativevalue;
unsigned char uc = c;
int i = uc;
printf("%d\n", i);

This is going to print a non-negative value. i == c is bound to compare
false.


The case I'm thinking of is the more usual one where a character is
assigned from input...

char text[100];
fgets(text, sizeof text, stdin);

The user might enter an extended character, é (e accent) say, which
would have a valid unsigned char value within an int, but (may) have a
negative representation as a (signed) plain char element of text on
conversion from int to char.

I assume (by expectation of reasonable QoI rather than the standard)
that the conversion from char to unsigned char would restore the
original character.

The vast majority of implementations I've been exposed to (limited I
grant you) with extended character support for non-C locales still
have signed plain char. In addition, these implementations convert a
_lot_ of characters to negative values within plain char. It would be
quite tediously difficult to use these implementations reliably, if
the conversion back to unsigned char did not restore the original
character.

--
Peter
Nov 14 '05 #16
Leor Zolman wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Do you prefer *my_string or my_string[0]? Why?


First off, I'm wondering whether you're asking about the limited case
of fetching just the first character of an array, or the more general
question "when is it better to traverse an array with a pointer vs.
using subscripts"? But for the most part, I think both questions share
the same answer [although the latter general question invites
performance-related arguments, most of which probably wouldn't stand
up to close scrutiny in this group ;-) ]


I have two questions:

1. Does the OP want to access an array or the first component
of that array? They are not the same thing.

2. Why does your software change the subject to ">"?

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #17
On Mon, 16 Feb 2004 20:13:08 GMT, CBFalconer <cb********@yahoo.com> wrote:
Leor Zolman wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
> Do you prefer *my_string or my_string[0]? Why?


First off, I'm wondering whether you're asking about the limited case
of fetching just the first character of an array, or the more general
question "when is it better to traverse an array with a pointer vs.
using subscripts"? But for the most part, I think both questions share
the same answer [although the latter general question invites
performance-related arguments, most of which probably wouldn't stand
up to close scrutiny in this group ;-) ]


I have two questions:

1. Does the OP want to access an array or the first component
of that array? They are not the same thing.

2. Why does your software change the subject to ">"?


Can't blame Agent for that one. I must've done it somehow.

Sorry for the delay-- just back from 6 days' vacation...and 1053 new
messages downloaded here. Is it any wonder some folks would prefer to not
expand the scope of the group?
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Christopher Benson-Manica | last post by:
If you had an unsigned int that needed to be cast to a const myClass*, would you use const myClass* a=reinterpret_cast<const myClass*>(my_val); or const myClass* a=(const myClass*)myVal; ...
33
by: amerar | last post by:
Hi All, I can make a page using a style sheet, no problem there. However, if I make an email and send it out to my list, Yahoo & Hotmail totally ignore the style tags. It looks fine in...
1
by: amerar | last post by:
Hi All, I posted a question about style sheets, and why certain email clients were ignoring them. Someone suggested placing them inline. I did this and get better results, but not what I...
4
by: KvS | last post by:
Hi all, I'm pretty new to (wx)Python so plz. don't shoot me if I've missed something obvious ;). I have a panel inside a frame, on which a Button and a StaticText is placed: self.panel =...
83
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include...
39
by: jamilur_rahman | last post by:
What is the BIG difference between checking the "if(expression)" in A and B ? I'm used to with style A, "if(0==a)", but my peer reviewer likes style B, how can I defend myself to stay with style A...
17
by: VeebeeGeeBees | last post by:
Why is it that you can do this in VB.net but not (the syntactical equivilent of it in) C#?: Dim m as MessageBox m.Show("hello")
3
by: SomeGuyHere | last post by:
I'm studying for the Microsoft .NET 2.0 Application development foundation exam. I just finished the 1000 page study guide cover-2-cover doing all examples. My first try I failed the practice test...
3
Claus Mygind
by: Claus Mygind | last post by:
I want to move some style setting into a javaScript function so I can make them variable but I get "invalid assignment left-hand side" error? see my question at the bottom of this message. It...
6
by: MatthewS | last post by:
I've seen the question raised several times here, but apparently never answered. Since PyInstance_Check returns False for new-style class instances, is there a standard procedure for testing this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.