Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          strings/knold-tot-c-like-3.cc - The Knold & Tot example with strcpy from <cstring>.Lecture 3 - slide 12 : 27
Program 4

// Use of strcpy - remedies the problem in the previous C program.
// Compiles and runs.

#include <iostream>
#include <cstring>    // Corresponds to <string.h> in a C program.

void g(){
  char s1[] = "Knold",
       s2[] = "Tot";

  std::strcpy(s1, s2);   // Works because "Tot" is not longer than "Knold".
  s2[1] = 'u';
  
  std::cout << s1 << " og " << s2 << std::endl;   // Tot og Tut
}

int main(){
  g();
}