Sunday, July 12, 2009

What is the output of C++ pointer variables with cout?

Consider the following program:





#include %26lt;iostream%26gt;





using namespace std;





void f(char* %26amp;c);


void g(char* myString);





int main()


{


char myString[10] = "Midterm";


char *tempPtr;





tempPtr=myString;


cout %26lt;%26lt; myString %26lt;%26lt; endl;


cout %26lt;%26lt; tempPtr %26lt;%26lt; endl;


g(myString);


g(tempPtr);


f(tempPtr);


cout %26lt;%26lt; myString %26lt;%26lt; endl;


cout %26lt;%26lt; tempPtr %26lt;%26lt; endl;


return 0;


}





void f(char* %26amp;s)


{


s=s+3;


cout %26lt;%26lt; s %26lt;%26lt; endl;


}





void g(char* myString)


{


myString=myString+5;


cout %26lt;%26lt; myString %26lt;%26lt; endl;


}





On gnu compilers, this is the output:





Midterm


Midterm


rm


rm


term


Midterm


term





Now, my question is, are there any implementations of C++ that only output what the pointer variable is pointing to? (i.e. not including the the following characters of the array?)





For example, are there any implementations that would make this be the output:





Midterm


M


rm


r


t


Midterm


t





Any light on how pointer variables are output will be appreciated!!!

What is the output of C++ pointer variables with cout?
No, cout will always print all the characters of char*'s until it hits a null.





You need to dereference the char* to an individual character before cout'ing it to get what you want:





char str[10] = "Midterm";


cout %26lt;%26lt; *(str+3) %26lt;%26lt; endl; // This outputs 't'
Reply:No.





And while cout function is included with most compilers it is NOT part of the language, but just a library of standard code people find useful to use.


(eg I can use c/c++ to code for my nokia phone but as there a gui and no commandline there no cout function provided in the dev kit!)





You need to change your code not the compiler of course!!! Just tested and MS Compiler gives same results, as you should expect!





C/C++ work with null terminated strings. And arrays are treated in many ways as little more than your tempPtr pointer.





So you reserved 10 bytes for your string, the first 7 chars are the letters then theres a 0/null byte, and the next 2 bytes are uninitilised(we cant be sure what values they could take at runtme.





Now cout treats any pointer to or array of chars the same it output the charaters in the order it finds them until it comes to a null valued byte. if we initialse out array as say {M','i','d','t','e','r','m'} we'd need to add a ,0 or cout would just keep going through memory writing what if finds until it eventually finds a zero or the program crashes(the OS should throw an exception if tries to read outside the part of memory given to your program)





If given a char though not a pointer to a char then it'll give just that char.





Eg.


cout *(myString+1);


prints only the letter "i"


and


cout %26amp;myString;


shows the memory address used for myString.





Remember the referencing and dereferencing operators?





Wait - but for more than one char to be output but not the whoile string you need a suitably sized buffer(the final substring size + 1 char for the null) and would memcopy the bytes into it and the set a null at the end before sending it to cout.


No comments:

Post a Comment