// ========================================================================== // $Id: char_pointer.cpp,v 1.7 2014/09/10 17:18:02 jlang Exp $ // CSI2372 example Code for lecture 2 // ========================================================================== // (C)opyright: // // Jochen Lang // SITE, University of Ottawa // 800 King Edward Ave. // Ottawa, On., K1N 6N5 // Canada. // http://www.site.uottawa.ca // // Creator: jlang (Jochen Lang) // Email: jlang@site.uottawa.ca // ========================================================================== // $Log: char_pointer.cpp,v $ // Revision 1.7 2014/09/10 17:18:02 jlang // Added char_num_pointer example. Changed to C++ brace initialization. // // Revision 1.6 2011/09/10 01:08:19 jlang // Updates F10 // // Revision 1.5 2008/09/08 21:48:32 jlang // Clearer size computation // // Revision 1.4 2007/09/12 18:36:59 jlang // Extra comments // // Revision 1.3 2007/09/12 16:59:31 jlang // Use of size_t // // Revision 1.2 2006/09/18 17:57:15 jlang // Hexadecimal output for address // // Revision 1.1 2006/09/07 20:06:17 jlang // Initial check-in lecture 0-2 // // Revision 1.2 2005/10/07 23:14:20 jlang // Lecture 4 // // Revision 1.1.1.1 2005/09/21 21:22:14 jlang // Initial check-in // // // ========================================================================== #include #include using std::cout; using std::endl; int main() { char sentence[]{"Old style C-string"}; char *ptrSentence; ptrSentence = &sentence[0]; // Size of array in bytes cout << "Array size (bytes): " << sizeof(sentence) << endl; // Size of dereferenced pointer in byte cout << "Size of *ptr (bytes): " << sizeof(*ptrSentence) << endl; // Content of first and last character plus terminal 0 cout << *ptrSentence << " to " << *(ptrSentence+17) << " plus " << (unsigned int) *(ptrSentence+18) << endl; // Addresses of first character and of terminal 0 // Need to make sure that is shows as address and not as string cout << std::hex << (size_t) ptrSentence << " to " << (size_t) (ptrSentence+18) << std::dec << endl; // Length in Bytes cout << "String Length: " << (size_t) (ptrSentence+17) - (size_t) ptrSentence + 1 << " (excluding terminal 0)" << endl; }