Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7219005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:33:54+00:00 2026-05-28T21:33:54+00:00

I was looking through the forums and saw a question about counting the numbers

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:33:55+00:00Added an answer on May 28, 2026 at 9:33 pm

    Very good try. There’s just one tiny thing wrong, which is this:

    p <= 3
    

    Pointers are just some number which represents a memory address. When you do p = alph, you’re not setting p to 0, you’re setting it to point to the same address as alph. 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:

    alph + 3 // is a pointer to one past the end of the array
    

    Then your loop becomes

    for (; p < alph + 3; ++p)
    

    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 + 3 is 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:

    /-----------------------------------\
    | Pointer Version |   Index Version |
    -------------------------------------
    |  p              |              i  |
    |  p = alph       |          i = 0  |
    |  *p             |        alph[i]  |
    |  alph + 3       |              3  |
    |  p < alph + 3   |          i < 3  |
    \-----------------------------------/
    

    Also note that instead of doing alph + 3, you may want to use sizeof. sizeof gives 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 with alph but not with p, for example). The advantage of using sizeof to 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 wrote alph + 3 and change them. You can do that like this:

    for (; p < alph + sizeof(alph); ++p)
    

    Additional note: because the size of char is defined to be 1 byte, this works. If you change the array to an array of int, 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:

    for (; p < alph + sizeof(alph) / sizeof(*alph); ++p)
    

    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

    int alph[3];
    

    Then sizeof(alph) == 12 because each int is 4 bytes big. sizeof(*alph) == 4 because *alph is the first element of the array. Then sizeof(alph) / sizeof(alph) is equal to 12 / 4 which is 3, which is the number of elements in the array. Then by doing

    for (; p < alph + sizeof(alph) / sizeof(*alph); ++p)
    

    that is equivalent to

    for (; p < alph + 12 / 4; ++p)
    

    which is the same as

    for (; p < alph + 3; ++p)
    

    Which is correct.

    This has the good advantage that if you change the array size to 50 and change the type to short (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::end to do all this work for you:

    for (; p < std::end(alph); ++p)
    

    Or you can just use the range-based for loop introduced in C++11:

    for (char c : alph)
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just looking through some information about Google's protocol buffers data interchange format.
I have been looking through Scott Guthrie's MVC tutorials and it seems like the
I've done some looking through the site and on the Internet trying to find
I was looking through different forums, but didn't find anything related to my small
I have searched through the google and also joomla forums but didn't got what
Through various questions I have asked here and other forums, I have come to
I have asked this question countless times on various forums. Ultimately all I need
I've been through the forums and read many posts relating to my question, but
hHi all! I have posted this question on the WP support forums, but the
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.