Display an array of characters both using a pointer and an array index - C++ Data Type

C++ examples for Data Type:char array

Description

Display an array of characters both using a pointer and an array index

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    const char* szString = "test test";
    cout << "The array is '" << szString << "'" << endl;

    cout << "Display the string as an array: ";

    for(int i = 0; i < 5; i++){
      cout << szString[i];/*from   www .ja  v a  2 s  .c  o m*/
    }
    cout << endl;

    cout << "Display string using a pointer: ";
    const char* pszString = szString;

    while(*pszString){
      cout << *pszString;
      pszString++;
    }
    cout << endl;

    return 0;
}

Result


Related Tutorials