So my teacher has a quiz question and his practice question for it is for us to code the strcpy function for character arrays assuming acceptable ones are passed. I have written my version and just want to see if it you see any errors in it. Or even if there's extra stuff happening that I don't know and should learn about. This is my first semester of C++ for reference.

char strcpy( char dest[], char src[] )
   {
    index = 0;

    while( src[index] != NULL_CHAR )
       {
        dest[index] = src[index];
        index++;
       }

    dest[index] = NULL_CHAR;

    return dest;
   }

Why did you define NULL_CHAR? Most people just use NULL definition that standard C header files already define. There is no point defining your own.

The function's return value shloud be char* (you need to add the star)

Why did you define NULL_CHAR? Most people just use NULL definition that standard C header files already define. There is no point defining your own.

The function's return value shloud be char* (you need to add the star)

Sorry, my teacher uses NULL_CHAR, I thought it was just a global constant from the standard library. There's one point cleared up, thanks.

As for the char*, we haven't covered pointers directly yet so I don't know what is going on there.

Since the function is returning a pointer you have to declare the function like that. Your compiler will not compile the program the way you have it now.

If your teacher wants you to use NULL_CHAR then do it. But you won'
t find that used anywhere else, probably including other teachers.

Alright, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.