| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Squares |
Hello Pascal!
Replying to a message of Pascal Schmidt to Darin McBride:
DM>> Nah - I like this one. I suppose it's a forest/trees thing - with an
DM>> index, you concentrate on the array (forest), and with a pointer,
DM>> you concentrate on the single object that you're dealing with on
DM>> this iteration through the loop (tree).
PS> I don't think of them as really different. Array notation is turned
PS> into a pointer expression by the compiler. One just needs to know the
PS> rules.
Functionally, they're the same. Conceptually, they're different.
DM>> It's also easier to write. Compare my_string[my_index] to *my_ptr.
PS> Which is not the same, *(my_ptr + my_index) would be the same as the
PS> array expression. Otherwise you'd have to compare *my_ptr and
PS> my_string[0] and increment my_string in a loop, which is what I
PS> assume you do with my_ptr.
Given that they're in loops, they're the same.
for (my_index = 0; my_index < my_length; my_index++)
{
/* use my_string[my_index] here */
}
for (my_ptr = my_string; *my_ptr != '\0'; ++my_ptr)
{
/* use *my_ptr here */
}
This is what we were talking about, so that's the context I was using. Of
course, if we ignore looping for a bit, and talk purely about arrays vs
pointers to arrays, where my_ptr == &my_string[0], then you're right.
DM>> char const* p
DM>> for (p = target; '\0' != *p; ++p)
DM>> {
DM>> /* modifications here to *p will be caught
DM>> * by the compiler!
DM>> */
DM>> }
PS> In this case p starts out as an alias to target and modifications via
PS> target can still screw up the loop.
Yes - if you modify target. The theory would be that finding where you
used "target" wrong in this loop is easier than in a loop with
indexes. ANYWHERE you use "target" in this loop is incorrect,
while you need to examine the index-based equivalent very, very closely to
figure out which use is wrong.
DM>> This is a bit more painful to do with indexes - or, at least a bit
DM>> less obvious. char const* p = target; for (index = 0; index <
DM>> strlen_of_target; ++index) { /* use p[index], not
DM>> target[index]... */ }
PS> Yeah, that looks bad.
Especially when the message editor wordwraps it. :-)
PS>>> However, I never needed to traverse a string this way in all the C
PS>>> programs I have written so far.
DM>> You're right - you don't *need* to do things this way. I still think
DM>> better ;->they're
PS> What I meant was that all the string traversal "by hand"
I needed so
PS> far was done via strchr(3) and strrchr(3).
Hmmm - fair enough. You've never needed to do encoding/decoding, obviously ;-)
Darin
---
* Origin: Tanktalus' Tower BBS (1:250/102)SEEN-BY: 633/267 270 @PATH: 250/102 99 10/345 106/1 2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.