| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Squares |
Hi Darin! :-)
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, you
DM> concentrate on the single object that you're dealing with on this
DM> iteration through the loop (tree).
I don't think of them as really different. Array notation is turned into a
pointer expression by the compiler. One just needs to know the rules.
DM> It's also easier to write. Compare my_string[my_index] to *my_ptr.
Which is not the same, *(my_ptr + my_index) would be the same as the array
expression. Otherwise you'd have to compare *my_ptr and my_string[0] and
increment my_string in a loop, which is what I assume you do with my_ptr.
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> }
In this case p starts out as an alias to target and modifications via
target can still screw up the loop.
DM> This is a bit more painful to do with indexes - or, at least a bit
DM> less obvious.
DM> char const* p = target;
DM> for (index = 0; index < strlen_of_target; ++index)
DM> {
DM> /* use p[index], not target[index]... */
DM> }
Yeah, that looks bad.
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
What I meant was that all the string traversal "by hand" I needed
so far was done via strchr(3) and strrchr(3).
Ciao
Pascal
--- Msged/LNX 6.1.1
* Origin: sed s/$(echo $HOME | sed 's/[[:punct:]]/\\&/g')/~/ (1:153/401.2)SEEN-BY: 633/267 270 @PATH: 153/401 307 140/1 106/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™.