copying strings

I am using the following code to copy strings from one array to another. Upto 7 characters it copies fine but when the source string increases from 7 it starts giving '@' sign at the end of the output. can somone figure out the problem?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void stringCopy (char destination[], char source[])
{
        for(int i=0; source[i]!='\0'; i++)
        {
                destination[i]=source[i];
        }
}
int main()
{
        int n;
        char source[]="abcdefghijkl";
        source[n];
        char destination[n];
        stringCopy(destination, source);
        cout << "Destination is " << destination << endl;
        return 0;

}
can somone figure out the problem?

You're not using std::string.
sorry i dont know its use. can u please explain what is it for? and where and how to use it?
1
2
3
4
5
6
int main()
{
  string source="abcdefghijkl";
  string destination=source;
  cout << destination << endl;
}


And http://www.cplusplus.com/reference/string/string/
i am making a "function", so cant really do all this stuff in main. Also i do not want to use string library
Well, in your original code snippet you're using n uninitialized. In fact, that's not even valid C++ and should not compile.
Out of interest, are you using DevC++ ?

In addition to the uninit var n, you're failing to null term the destination, as your copy loop stops at the null in the source.
To Athar(2663)
n gets the value from source as, which means, that whatever is the length of source is, would be the value of n. so, nothing is wrong with n. and yes my code does compile and gives no error.
To andywestken(935)
no i am not using devC++
n gets the value from source as, which means, that whatever is the length of source is, would be the value of n.

No, you never assign any value to n.

And what did you expect source[n] to do? That's a no-op.

and yes my code does compile and gives no error.

It's still not valid C++. An array size must be a constant known at compile-time.
The only reason it compiles is because you actually do define the size of the arrays; but not in the way that you think. source[] is defined by actually putting in the variables, so it is exactly the length of the input that you put in to it. destination[n] with an undefined int n actually makes destination fixed at exactly the length that the compiler just happens to select (by selecting a probably already initialized memory address), and is not actually defined as a known parameter. So, while it compiles, you still don't get the result that you're looking for, and probably never will.
Unfortunately gcc "C++" compilation supports C99 array defs, by default, so the size doesn't have to be constant. Of course, it's incorrect C++.

(I asked about DevC++ as it seems to be popular, and uses gcc. VC++ needs constant array sizes)
Last edited on
yes variable n has no point and as far as the function goes it could be this simple

1
2
3
4
5
6
7
void stringCopy (char destination[], char source[])
{
       
        
                destination[]=source[];
        
}
Topic archived. No new replies allowed.