tags:

views:

181

answers:

5

I am using the following code to copy the string contents to another string. Two logics are used ,One with while loop(commented) is working and the other is not (as=at).

Please help me to identify the flaw in this code.

Thanks in advance


#include<stdio.h>
#include<conio.h>
main()
{
    char *s="SourceString";
    char *t="TargetString";

    void print(char *s);
    void strcopy(char *s,const char *t);
    print(s);

    strcopy(s,t);
    print(s);
    getch();
}

void strcopy(char *as,const char *at)
{
    /*while((*as=*at)!='\0')    // working
    {
        as++;
        at++;
    }  */
    as=at;    //not working
}

void print(char *s)
{
    printf("\n Printing the Contents:");
    for(;*s!='\0';s++)
        printf("%c",*s);
    printf("\n END");
}

All are saying that the strings are passed by Value and not by address. But I am passng the address of the strings in calling functiona nd in called function am using pointers. Please clarify I am new to pointers

+2  A: 

When as is a function argument, writing as=anything; ceases to have effect when the function terminates, because its arguments cease to exist (as well as its local variables). You are not modifying the string, you are only modifying a memory cell that was temporarily containing the address of the string, and which was going to be released anyway.

Your question is really a question about pointers. Perhaps you can find an already asked question on StackOverflow about pointers in C or C++ that helps you.

Pascal Cuoq
+1  A: 

The version that works uses a loop to copy one character at a time.

The version that doesn't just copies one pointer variable to another. This operation doesn't affect anything else in the program. as = at just modifies one local variable and returns. So that is not an optimization that you can make.

Potatoswatter
A: 

With as=at; you copy local variable into local variable. Never mind that they are pointers - they are copies on the function's stack, so your changes are not visible outside the function.

Nikolai N Fetissov
+2  A: 

Assigning a string like this:

char *s="SourceString";

is using readonly memory so modifing the contents will result in undefined behaviour. Using arrays will probably work as you thought.

char s[]="SourceString";
char t[]="TargetString";

In your example, you are just playing with local variables which get destroyed when the strcpy function returns.

You could try something like this:

void strcopy2(char **s, char **t) //<--- pointers to pointers, ouch!
{
    *t = *s; //<--- Assign the value of the pointer who's address was passed.
}

int main(void)
{
    char *s = "SourceString";
    char *t = "TargetString";

    printf("%s\n", s);
    strcopy2(&s, &t);  //<--- pass address of pointers!
    printf("%s\n", t);

}

I prefer to use char arrays though if i'm manipulating strings.

Gary Willoughby
Oh, good point. And double good point for noticing that the string "TargetString" was the one that was read and "SourceString" the one written to.
Pascal Cuoq
A: 

Here are two errors with your non working code.

1) Your statement as = at copies a local variable onto another local. The assignment is lost once you leave the function.
2) You are probably leaking whatever as pointed to before the assignment,

Another potential problem in usage is that usually in C we say target <- source so that your usage of as and at looks backwards.

Liz Albin