c++ - How to use pointers in for loops to count characters? -
c++ - How to use pointers in for loops to count characters? -
i looking through forums , saw question counting numbers of each letter in string. teaching myself , have done research , starting projects. here have printed elements of array. without pointers. know can utilize pointer array , have increment each value, need help doing so.
here code without pointer:
code main() { char alph [] = {'a', 'b', 'c'}; int i, o; o = 0; for(i=0; < 3; i++) { cout << alph[i] << ' '; }; };
here bad code doesn't work trying pointer work.
main() { char alph [] = {'a', 'b', 'c'}; char *p; p = alph; (; p<=3; p++); cout << *p; homecoming 0; };
i hope it's not obvious of answer; don't mean waste anyone's time. first post if wants give me advice, give thanks you.
very try. there's 1 tiny thing wrong, this:
p <= 3
pointers number represents memory address. when p = alph
, you're not setting p
0
, you're setting point same address alph
. when looping on array pointers, have compare current pointer pointer 1 past end of array. pointer 1 element past end of array, add together number of elements array:
alph + 3 // pointer 1 past end of array
then loop becomes
for (; p < alph + 3; ++p)
you may think getting pointer 1 past end of array going out of bounds of array. however, you're free pointer anywhere in memory, long don't dereference it. since pointer alph + 3
never dereferenced - utilize marker end of array - , fine.
here rough correlations different versions:
/-----------------------------------\ | pointer version | index version | ------------------------------------- | p | | | p = alph | = 0 | | *p | alph[i] | | alph + 3 | 3 | | p < alph + 3 | < 3 | \-----------------------------------/
also note instead of doing alph + 3
, may want utilize sizeof
. sizeof
gives number of bytes object occupies in memory. arrays, gives number of bytes whole array takes (but doesn't work pointers, can alph
not p
, example). advantage of using sizeof
size of array if alter size later, not have go , find places wrote alph + 3
, alter them. can this:
for (; p < alph + sizeof(alph); ++p)
additional note: because size of char
defined 1
byte, works. if alter array array of int
, illustration (or other type bigger 1 byte) not work more. remedy this, split total size in bytes of array size of single element:
for (; p < alph + sizeof(alph) / sizeof(*alph); ++p)
this may little complicated understand, you're doing taking total number of bytes of array , dividing size of single element find the number of elements in array. note adding the number of elements in array, not the size in bytes of array. consequence of how pointer arithmetic works (c++ automatically multiplies number add together pointer size of type pointer points to).
for example, if have
int alph[3];
then sizeof(alph) == 12
because each int
4 bytes big. sizeof(*alph) == 4
because *alph
first element of array. sizeof(alph) / sizeof(alph)
equal 12 / 4
3
, number of elements in array. doing
for (; p < alph + sizeof(alph) / sizeof(*alph); ++p)
that equivalent to
for (; p < alph + 12 / 4; ++p)
which same as
for (; p < alph + 3; ++p)
which correct.
this has advantage if alter array size 50
, alter type short
(or other combination of type , array size), code still work correctly.
when more advanced (and understand arrays plenty stop using them...) larn how utilize std::end
work you:
for (; p < std::end(alph); ++p)
or can utilize range-based for
loop introduced in c++11:
for (char c : alph)
which easier. recommend understanding pointers , pointer arithmetic before reclining on convenient tools of standard library though.
also job so, syntax-highlighted ascii-art-chart.
c++
Comments
Post a Comment