count no of characters

This is a discussion on count no of characters within the C++ Programming forums, part of the General Programming Boards category; Code: // Ex4_10.cpp // Counting string characters using a pointer #include <iostream> using std::cin; using std::cout; using std::endl; int main() ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    count no of characters

    Code:
    // Ex4_10.cpp
    // Counting string characters using a pointer
    #include <iostream>
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
    {
       const int MAX = 80;                 // Maximum array dimension
       char buffer[MAX];                   // Input buffer
       char* pbuffer = buffer;             // Pointer to array buffer
    
       cout << endl                        // Prompt for input
            << "Enter a string of less than "
            << MAX << " characters:"
            << endl;
    
       cin.getline(buffer, MAX, '\n');     // Read a string until \n
    
       while(*pbuffer)                     // Continue until \0
          pbuffer++;
    
    
       cout << "\nAddress of pbuffer: " << reinterpret_cast<size_t>(&pbuffer);
       cout << "\nAddress of buffer: " << reinterpret_cast<size_t>(&buffer) << endl;
    
       cout << "\n\nAddress of pbuffer: " << &pbuffer;
       cout << "\nAddress of buffer: " << &buffer << endl;
       
    
       cout << endl
            << "The string \"" << buffer
            << "\" has " << pbuffer - buffer << " characters.";
       cout << endl;
       return 0;
    }
    How does this count the number of characters ? its the code from a book which I'm reading, although I have added few statements in this

    Well pbuffer would be pointing at the last character after the while loop which I think is the null character ('\0')

    While buffer is pointing at the first element in the array

    So its simple that its difference would produce the number of characters,

    I checked its address and on my pc it produced

    the following

    pbuffer = 1244920
    buffer = 1244932

    in hex

    pbuffer = 0012FEF8
    buffer = 0012FF04


    so by manual calc of the above values their difference is 12, Right ?

    But when used in the cout << statement its not the same ?

    how ?
    Last edited by manzoor; 08-27-2008 at 08:55 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Strange, as the posted code works OK for me:
    Code:
    Enter a string of less than 80 characters:
    Hello, World!
    Address of buffer: 2293520
    Address of pbuffer: 2293516
    The string "Hello, World!" has 13 characters.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Well what I meant is that the difference between the addresses

    2293516 - 2293520 = -4

    then how is it showing 13 in the cout statement


    sorry but i think im not able to express what i want to say, so please state if you didn't get the point
    Last edited by manzoor; 08-27-2008 at 08:57 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, in the original code you had:
    Code:
       cout << "Address of buffer: " << reinterpret_cast<size_t>(&buffer) << endl;
       cout << "Address of pbuffer: " << reinterpret_cast<size_t>(&pbuffer);
    If you remove the &, you will get more sane results:
    Code:
    Enter a string of less than 80 characters:
    12345
    Address of buffer: 2293520
    Address of pbuffer: 2293525
    The string "12345" has 5 characters.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Code:
       cout << "\n\nAddress of pbuffer: " << &pbuffer;
       cout << "\nAddress of buffer: " << &buffer << endl;
    Well I used the & because if I just cout << pbuffer, it would show the character at which it is pointing

    So without using the reinterpret_cast how would one return the address at which pbuffer and buffer are pointing?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, it won't print the character - it would print the STRING it's pointing to.

    As to how you get the address - you would have to cast it somehow if you use cout >> to get the value of a char pointer - you could for example cast it to another pointer [void * would be my choice].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Ok it prints the string its pointing to

    how to get the address of that string without using the reinterpret_cast

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manzoor View Post
    Ok it prints the string its pointing to

    how to get the address of that string without using the reinterpret_cast
    Sorry, I was editing my reply whilst you were writing this one. You have to cast it some way or another - that's because operator>>(ostream &os, const char *) is defined to print a string - all other operator>>(ostream &os, T *) are defined such as this:
    Code:
    template <typename T> ostream & operator>>(ostream &os, T *ptr)
    {
         os << hex << reinterpret_cast<int_ptr_t>(ptr);
    }
    Edit: it's the same type of problem as if you want to print the numeric value of a char - it will print the corresponding character unless you cast it to a different type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Oh alright, no problem matsp

    Thanks that helped me out.

    You and this community have helped me too much.

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 07:41 PM
  2. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  3. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 07:05 AM
  4. Convert string of characters to integers
    By Digital_20 in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2008, 10:40 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 08:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21