Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) StdStrncpyExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppStdStrncpyExample1/CppStdStrncpyExample1.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

 

 

 

 

 

./CppStdStrncpyExample1/main.cpp

 

#include <cassert>
#include <cstring>
#include <iostream>

int main()
{
  const char s[] = "0123456789";

  const int sz = static_cast<int>(std::strlen(s));
  assert(sz == 10);
  for (int i=0; i!=sz + 1; ++i) //+1 because both empty and full string are shown
  {
    char * const t = new char[i + 1]; //+1 for null terminator
    std::strncpy(t,s,i);
    t[i] = '\0';
    std::cout << t << '\n';
    delete[] t;
  }
}

/* Screen output:


0
01
012
0123
01234
012345
0123456
01234567
012345678
0123456789
Press <RETURN> to close this window...

*/

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml