C
Christopher Benson-Manica
Do you prefer *my_string or my_string[0]? Why?
Christopher Benson-Manica said:Do you prefer *my_string or my_string[0]? Why?
Tom St Denis said:Christopher Benson-Manica said:Do you prefer *my_string or my_string[0]? Why?
depends. somestruct->charptr[0] is easier to read than
*(somestruct->charptr) I think...
Tom St Denis said: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.
Charles Harrison Caudill said:I'm assuming then that the '->' binds more tightly than the '*'?
Charles Harrison Caudill said:Tom St Denis said: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 '*'?
Do you prefer *my_string or my_string[0]? Why?
int startsWithId(char const str[])
{
return isupper(str[0]) || str[0] == '_'; // consistent
// or (pick ONE):
return isalpha(*str) || *str == '_'; // not consistent?
}
int startsWithId(char const str[])
{
return isupper(str[0]) || str[0] == '_'; // consistent
// or (pick ONE):
return isalpha(*str) || *str == '_'; // not consistent?
}
Do you prefer *my_string or my_string[0]? Why?
In said:Do you prefer *my_string or my_string[0]? Why?
Leor Zolman said: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.)
Peter said:Leor Zolman said: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.
Richard Heathfield said:Peter said:Leor Zolman said: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,
Huh?
and you've learned something
important about ch.
}
Peter said:Richard Heathfield said: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.]
Richard Heathfield said:Peter said:Richard Heathfield said: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.
Leor said:Christopher Benson-Manica said: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 ;-) ]
Leor said:Christopher Benson-Manica said: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 ">"?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.