Arrays of Characters in Cpp

/******************************************************************************
 * Dealing with arrays of characters                                 main.cpp *
 *                                                                            *
 *  Author: nicomp                                                    *
 *                                                                            *
 * Abstract:                                                                  *
 * An array of characters is often called a string.                           *
 *                                                                            *
 * There is also a C++ data type called a String, which will be introduced    *
 *   later in the course.                                                     *
 *                                                                            *
 *  Terminology:                                                              *
 *         0 or '\0' is a null temninator. Refer to the ASCII chart.          *
 *         a 'string' is a null-terminated array of characters                *
 *         'n' is a single character. Use single quotes.                      *
 *         "n" is a string. Use double quotes.                                *
 *         Any text enclosed in double quotes automagically gets              *
 *           a null terminator at the end.                                    *
 ******************************************************************************/
#include <iostream>
using namespace std;

void main()
{
//    A null-terminated array of characters is often called a string in C++
//                   0123456789012345
    char buff[] = "Hello World";    // buff is how long?
    char tmp[] = "Goodbye";

    cout << "\n<" << buff << ">";    // How does cout know when to stop printing?

//    What if we clobber the null terminator?
    buff[11] = '?';        // Note: single chatacter uses single quotes

//    This is the same print logic that we used a few lines earlier. 
//    We should get different results because we replaced the null terminator.
    cout << "\n<" << buff << ">";    // How does cout know when to stop printing?

    cout << "\n\n\n";
}


More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    C Programming Language

    Click to Rate This Article
    working