Thread: Question about memory address

  1. #1
    Registered User
    Join Date
    Aug 2004

    Question about memory address

    Hi I have another question. I was reading my tutorial and there's a sample program to count the number of characters an user entered with a pointer array.
    // Ex3_10.cpp
    // Counting string characters using a pointer
    #include <iostream>
    using namespace std;
    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
       cout << endl
            << "The string \"" << buffer
            << "\" has " << pbuffer-buffer  << " characters.";
       cout << endl;
       return 0;
    What I don't get is the bolded part, pbuffer-buffer. How does that output the number of characters? Wouldn't that show the bytes of memory the string takes up since they point to memory address?
    Thanks in advance for whoever answers this.

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    A char is equal to 1 byte always. So it works out to be the same.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001

    Exclamation Pointer Arithmetic

    A char is equal to 1 byte always.
    WRONG! (Sorry, manofsteel972)

    The language standard requires that a char hold at least one byte (8 bits).

    Actually, this has to do with Pointer Arithmetic. This is a feature of the language that lets you, for example, incriment pointers without worrying about the size-of the object being pointed-to.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    The language standard requires that a char hold at least one byte (8 bits).
    That may be what the standard says, but how exactly the compiler implements the standard is... well.... compiler specific. Most compilers that I know of use 1 byte and 1 byte only.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    It doesn't matter what type you have, you get the same answer

    int array[10];
    int *p1 = array;
    int *p2 = &array[8];
    ptrdiff_t n = p2 - p1;
    n will have the value 8, no matter what type you choose.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing variables with memory address
    By ITAmember in forum C Programming
    Replies: 54
    Last Post: 06-28-2009, 03:35 AM
  2. char ** and argv memory question
    By simo_mon in forum C Programming
    Replies: 3
    Last Post: 08-17-2008, 08:47 AM
  3. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  4. hardbinding a memory address to a variable in code
    By quickNitin in forum C Programming
    Replies: 3
    Last Post: 07-24-2006, 01:09 AM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM