hey guys I need help with this I have no idea how to work this out please help me with like atleast the starting coding i am trying to google too but i cannot find any info on this.

"char * copyString( const char * s );
copyString returns a dynamically allocated copy of the arg string.
For instance, if s were “CAT”, then copyString allocated a 4-byte array, sets that array to “CAT”, and returns the address of the first element of the dynamically allocated array. As usual, deallocation is the caller’s responsibility."

this is the function that i am suppose to have please can some one point me in the right direction.
thank you

Recommended Answers

All 6 Replies

What part are you having trouble with? The details are all there in order that you need to do them. If you simply follow directions you will get through.

Okay so i pass both arrays then create the new Array of char right ???? how big is the new array suppose to be ??? and how do i copy those two arrays seprately in the new ARRAY???

i am trying to google too but i cannot find any info on this.

You may have better luck if you search for "strdup implementation". Your copyString() function already has a de facto name of strdup. The implementation is dead easy, but you'll need to remember to call free() on the returned pointer when using that function.

Also note that strdup is not a good name as it invades the compiler's reserved name space.

char * copyString( const char * s )
{
    if (NULL == s)
    {
        return NULL;
    }
    char *str = (char*)malloc(strlen(s)+1);
    memset(str,0,strlen(s)+1);
    strcpy(str,s);
    return str;
}
commented: At least, it's good code. +5

Since you are new, suquan629, I'll offer a couple of pointers (no pun intended):

  1. You don't need to memset the array to 0 since you've allocated it to be exactly as long as you need
  2. Since this is a learning exercise, don't use strcpy(), instead use a loop (which is what strcpy() does internally anyway)
  3. Please don't solve users' problems for them. There are always people posting essentially "do my homework for me" in the guise of a question. Instead, provide hints that allow them to solve their problems themselves.
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.