help with pointers/chars

This is a discussion on help with pointers/chars within the C++ Programming forums, part of the General Programming Boards category; ok i need help. read the code Code: #include <iostream> using namespace std; int main() { const int MAX = ...

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Question help with pointers/chars

    ok i need help. read the code

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	const int MAX = 80;
    	char buffer[MAX];
    	char* pbuffer = buffer;
    	int i =0;
    
    	cin.getline(buffer, MAX, '\n');
    
    
    	while(*pbuffer)
    	{
    		pbuffer++;
    	}
    
    	cout << "num of chars in buffer is " << (pbuffer - buffer);
    
    	return 0;
    }
    why is (pbuffer - buffer) the number of characters in the buffer array. i know at the end of the program execution pbuffer has the address of '\0', but why does subtracting the address of '\0' to the buffer array equal the number of characters in the buffer array?

    please do tell.!!!

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    In the beginning you set pbuffer and buffer to the same memory address.

    Then you put a string there ( so therefore pbuffer and buffer are both the address of the first char )

    then you changed pbuffer to the address of end of the string ( with the while loop )

    so you were just cout'ing the differnence in the address of the beginning of the string and the end of the string.

  3. #3
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Question also another question

    why does this work

    Code:
    char *pbuffer = NULL;
    cin >> pbuffer;
    cout << pbuffer;
    and this doesnt work
    Code:
    char *pbuffer = NULL;
    cin.getline(pbuffer, 80, '\n');
    please explain...!
    nextus, the samurai warrior

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    omg..thanks for clearing up for the first question..i thought the pepsi was getting to me...hehe...i havent sleep for almost a day..
    nextus, the samurai warrior

  5. #5
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    so...

    char* string = "blah";
    creates a literal string "blah" and stores it in a const char array which i cannot do string = "blah3" because string is pointing to the address of "blah" and i cannot make it point to "blah3" because there is no memory location of "blah3". if this is correct. thanks i understand now.....if now please explain further
    nextus, the samurai warrior

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Code:
    char * s = "hello"; /* compiler allocates "hello" off the stack and points 's' at it. */
    
    
    s = "world"; /* no problem, compiler cleans up stack literal "hello" */
    
    char * t = s; // fine
    
    strcpy(t, "bye"); // crash! "world" is const!
    So in other words, the pointer is not const here, the literal itself is.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Ah, thanks for setting me straight Salem!
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

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