skrowten_hermit
skrowten_hermit

Reputation: 445

String copy in C using pointers doesn't show expected result

I was trying to implement a string copy function using pointers as shown below:

#include <stdio.h>
#include <stdlib.h>

void copyStringPtr(char *from, char *to);

int main()
{
    char strSource[] = "A string to be copied.";
    char strDestination[50];

    copyStringPtr(strSource, strDestination);       // Doesn't work.

    return 0;
}

void copyStringPtr(char *src, char *dst)
{
    printf("The destination was\t:\t%s\n", dst);

//    for(; *src != '\0'; src++, dst++)
//        *dst = *src;

    while(*src)             //  The null character is equivalent to 0, so when '\0' is reached, the condition equals to 0 or false and loop is exited.
        *dst++ = *src++;

    *dst = '\0';

    printf("The source is\t\t:\t%s\n", src);
    printf("The destination is\t:\t%s\n\n", dst);
}

The expected output is :

The destination was :   
The source is       :   A string to be copied.
The destination is  :   A string to be copied.

But the output I'm getting is :

The destination was :   
The source is       :   
The destination is  :   

As it can be seen, even the source doesn't seem to have the initialized value. What am I doing wrong here?

Upvotes: 1

Views: 182

Answers (4)

Wayne Taylor
Wayne Taylor

Reputation: 51

#include <stdio.h>
#include <stdlib.h>

void copyStringPtr(char *from, char *to);

int main()
{
    char strSource[] = "A string to be copied.";
    char strDestination[50] = {0};

    copyStringPtr(strSource, strDestination);

    return 0;
}

void copyStringPtr(char *src, char *dst)
{
    char const *const dstOriginal = dst;

    if (!src || !dst){
        return;
    }

    printf("The source is\t\t:\t%s\n", src);

    while(src && *src)
        *dst++ = *src++;
    *dst = '\0';

    printf("The destination is\t:\t%s\n\n", dstOriginal );
}

Upvotes: 0

skrowten_hermit
skrowten_hermit

Reputation: 445

I just had to make a minor modification as follows:

#include <stdio.h>
#include <stdlib.h>

void copyStringPtr(char *from, char *to);

int main()
{
    char strSource[] = "A string to be copied.";
    char strDestination[50];

    copyStringPtr(strSource, strDestination);       // Doesn't work.

    printf("The source is\t\t:\t%s\n", strSource);
    printf("The destination is\t:\t%s\n\n", strDestination);

    return 0;
}

void copyStringPtr(char *src, char *dst)
{
    const char *srcOriginal = src;
    const char *dstOriginal = dst;

    printf("The destination was\t:\t%s\n", dstOriginal);

//    for(; *src != '\0'; src++, dst++)
//        *dst = *src;

    while(*src)             //  The null character is equivalent to 0, so when '\0' is reached, the condition equals to 0 or false and loop is exited.
        *dst++ = *src++;

    *dst = '\0';
}

And it worked just fine. As pointed out by @Jabberwocky in his comment, the pointers *src and *dst in copyStringPtr had moved to the end of the character string and was pointing to the NULL terminator.

Upvotes: 1

Inian
Inian

Reputation: 85560

Just take a backup of the original pointers before doing the increment. The reason you lose those pointers is, as part of the iteration in the while the start address of the string is lost and the printf() function does not know where the string starts. Since both the original pointers point at \0 at the end of the loop, the printf() function does not see a an actual string to print up-to

You can even make these backup pointers const to disallow modifications made to them.

void copyStringPtr(char *src, char *dst)
{
    printf("The destination was\t:\t%s\n", dst);
    /*
     * backing up the original source/desination 
     * pointers
     */
    const char *b_src = src; const char *b_dst = dst;

    while(*src)             //  The null character is equivalent to 0, so when '\0' is reached, the condition equals to 0 or false and loop is exited.
        *dst++ = *src++;

    *dst = '\0';

    printf("The source is\t\t:\t%s\n", b_src);
    printf("The destination is\t:\t%s\n\n", b_dst);
}

Also as noted in Blaze's answer calling printf() on a uninitialized character array invokes undefined behavior.

Upvotes: 1

Blaze
Blaze

Reputation: 16876

One issue is that you're not initializing char strDestination[50];. Thus it doesn't represent a valid string and when you try to print it here:

printf("The destination was\t:\t%s\n", dst);

It is undefined behavior. You can initialize it like this:

char strDestination[50] = {'\0'};

This explicitly sets the first char to '\0', making it a valid string. And the rest of the array is then default-initialized to '\0' anyway.

Also, after the while loop, your src and dst will point to the null terminator at the end of the strings, so when you print them, it prints nothing. Instead, keep a copy of the original pointers and print those instead:

void copyStringPtr(char *src, char *dst)
{
    char* srcOriginal = src;
    char* dstOriginal = dst;
    ...
    printf("The source is\t\t:\t%s\n", srcOriginal);
    printf("The destination is\t:\t%s\n\n", dstOriginal);
}

Upvotes: 3

Related Questions