Im trying a program that asks the user to enter his 1st name
to store it in an array of char that has 20 size.
then declaring another array of the same type and size
to copy the 1st string to the second array
using function.
#include <iostream>
#include <cstring> //We need this for strcpy
usingnamespace std;
int main()
{
constint SIZE = 20;
char name[SIZE]; //This will hold the name
char name_copy[SIZE]; //We will copy the name to this
cout << "Enter your first name: ";
cin >> name;
cout << "\nName is " << name << " eh?" << endl;
strcpy(name_copy,name); //Copies name to name_copy
cout << "name_copy is now " << name_copy << " too!" << endl;
return 0;
}
char * foo;
*foo // dereference a pointer
// is equivalent to
foo[0] // dereference a pointer
// and the type of the result is obviously
char
// one character. Not a string. Not an array. Not a pointer.
There is no assignment operator that would copy contents of one array into an another array.
There is std::copy and there are multiple C-string-aware copy functions. However, if your task is to write copystr() on your own, then you must use the basic constructs for educational purposes.
You do have a logical problem on the input as well; Every person in the world has 20 characters in their forename. Or do they? Your program thinks so.
How long is a piece of string? C-strings have a convention about that.