Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > Visual C++
|
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 20th, 2003, 06:18 PM
Registered User
 
Join Date: Aug 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default New to C++. Question about pointers

I'm using the textbook Beginning Visual C++ 6.0 and so far I find it very good. I'm in chapter 4 and I think I understand pointers, but I have a question about example 10. I've listed the code below. In the 'while loop' a pointer is being incremented. I understand what that is doing, but is there a way, using a 'cout' command to display the address of pbuffer for each character. I'm able to see them by installing a break point and watching pbuffer, but I was wondering if there is a way to code that output?

Thanks.



// EX4_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 << "\nEnter a string of less than " // Prompt for input
         << MAX << " characters: \n";

    cin.getline(buffer, MAX, '\n'); // Read a string until \n

    // The only way I could figure out how to get the value of
    // pbuffer, was to run the program in debug mode and
    // install a break point at the 'cout' statement following
    // this 'while loop'.
    while(*pbuffer) // Continue until \0
    {
        pbuffer++;
    }

    // pbuffer - buffer calculates the length of the string.
    // pbuffer will contain the address of the last character in the string.
    // buffer will contain the address of the first character in the string.
    // When you subtract them you get the length of the string.
    // In my case the addresses were: pbuffer = 0012FF37 and buffer = 0012FF2C
    // The string I entered was 'Hello World'. When you subtract the two addresses
    // you get 11 decimal.
    cout << "\nThe string \"" << buffer
         << "\" has " << pbuffer - buffer << " characters." << "\n";

    return 0;
}
 
Old August 21st, 2003, 10:28 PM
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I'm on Chapter 16.

For cout, the << operator is defined such that when a pointer to a char is on the right side(or an address of a char), the value at that address is displayed instead of the address.

If you try an example and use an int array, then you will be able to ouput the addresses using cout<<. That's because for cout, the operator << is defined differently when there is a pointer to an int on the right hand side.





Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ Pointers reality_42000 C++ Programming 1 March 4th, 2007 01:26 AM
array and pointers amrinder C++ Programming 1 November 10th, 2006 07:58 AM
pointers Stuby085 Visual C++ 1 August 30th, 2003 11:58 PM
Pointers and Arrays C++ odie All Other Wrox Books 1 July 10th, 2003 01:45 PM
Pointers jake VB How-To 12 June 21st, 2003 11:30 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.