I was looking through the forums and saw a question about counting the numbers of each letter in a string. I am teaching myself and have done some research and am now starting to do projects. Here I have printed the elements of the array. But I do so without pointers. I know I can use a pointer to an array and have it increment for each value, but I need some help doing so.
Here is my code without the pointer:
code main() {
char alph [] = {'a', 'b', 'c'};
int i, o;
o = 0;
for(i=0; i < 3; i++)
{ cout << alph[i] << ' '; };
};
Here is my bad code that doesn’t work trying to get the pointer to work.
main() {
char alph [] = {'a', 'b', 'c'};
char *p;
p = alph;
for (; p<=3; p++);
cout << *p;
return 0;
};
I hope that it’s not too obvious of an answer; I don’t mean to waste anyone’s time. Also this is my first post so if anyone wants to give me advice, thank you.
Very good try. There’s just one tiny thing wrong, which is this:
Pointers are just some number which represents a memory address. When you do
p = alph, you’re not settingpto0, you’re setting it to point to the same address asalph. When looping over an array with pointers, you have to compare the current pointer with a pointer that is one past the end of the array. To get a pointer to one element past the end of the array, you add the number of elements to the array:Then your loop becomes
You may think that getting a pointer to one past the end of the array is going out of bounds of the array. However, you’re free to get a pointer to anywhere in memory, as long as you don’t dereference it. Since the pointer
alph + 3is never dereferenced – you only use it as a marker for the end of the array – and everything is fine.Here are some rough correlations for the different versions:
Also note that instead of doing
alph + 3, you may want to usesizeof.sizeofgives you the number of bytes that an object occupies in memory. For arrays, it gives you the number of bytes the whole array takes up (but it doesn’t work with pointers, so you can do it withalphbut not withp, for example). The advantage of usingsizeofto get the size of the array is that if you change the size later, you do not have to go and find all the places where you wrotealph + 3and change them. You can do that like this:Additional note: because the size of
charis defined to be1byte, this works. If you change the array to an array ofint, for example (or any other type that is bigger than 1 byte) it will not work any more. To remedy this, you divide the total size in bytes of the array with the size of a single element:This may be a little complicated to understand, but all you’re doing is taking the total number of bytes of the array and dividing it by the size of a single element to find the number of elements in the array. Note that you are adding the number of elements in the array, not the size in bytes of the array. This is a consequence of how pointer arithmetic works (C++ automatically multiplies the number you add to a pointer by the size of the type that the pointer points to).
For example, if you have
Then
sizeof(alph) == 12because eachintis 4 bytes big.sizeof(*alph) == 4because*alphis the first element of the array. Thensizeof(alph) / sizeof(alph)is equal to12 / 4which is3, which is the number of elements in the array. Then by doingthat is equivalent to
which is the same as
Which is correct.
This has the good advantage that if you change the array size to
50and change the type toshort(or any other combination of type and array size), the code will still work correctly.When you get more advanced (and hopefully understand arrays enough to stop using them…) then you will learn how to use
std::endto do all this work for you:Or you can just use the range-based
forloop introduced in C++11:Which is even easier. I recommend understanding pointers and pointer arithmetic well before reclining on the convenient tools of the Standard Library though.
Also good job SO, you properly syntax-highlighted my ASCII-Art-Chart.