Copying string using pointers

Can somebody tell me what is wrong with the following code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  
void stringcpy(const char* src,char* dest){
    
    while(*src != '\0'){
        *dest++ = *src++;    
    }
}

int main()
{   
   const char str[] = "fatima";
    char str2[] = "" ;
    
   const char* s  = str;
    char* s2 = str2;
    
    stringcpy(s, s2);
    cout<<str2<<" ";
    cout<<str;
}
str2 can hold only 1 character. You are trying to write 7 characters: buffer overflow and possible crash or data corruption.
so what should i change in the above code , the actual question is :

Write a program to make a copy of the given string.
write one function strcpy1(char *src, char * dest), that makse a copy of str using arrays.
The destination str2 does not provide enough space to receive the string from the source str
Is there a way to do something like :

char str2[str.length()];

I know the above is wrong ….anything similar i could do ?
Is there a way to do something like
Yes, use C++ string instead of null-terminated character arrays.

In your case you need to dynamically allocate memory for your string: http://www.cplusplus.com/doc/tutorial/dynamic/

Or if your strings are compile time constants, you can use:
1
2
char x[] = "Hello";
char y[sizeof(x)];
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

void stringcpy(const char* src,char* dest){
    
    while(*src != '\0'){
        *dest = *src;
        *dest++;
        *src++;
    }
}

int main()
{   
    const char* str = "fatima";
    char* s2;
    
    stringcpy(str, s2);
    cout<<str<<" "<< s2;
}
@kemort: even worse. s2 is not even valid pointer, and why did you change his function which was completely fine and actually resembles real implementation?
closed account (48T7M4Gy)
I dunno pal. Mine works his doesn't. Just call me old-fashioned.
Mine works
Just crashed for me. Your solution is an undefined behavior and any compiler would scream at you
main.cpp|18|warning: 's2' is used uninitialized in this function [-Wuninitialized]|


Also your stringcpy function just does not work because you never bother to copy characters after first.
closed account (48T7M4Gy)
Works on the gear wheel.
Works on the gear wheel.

working on Xcode 5.1 but i know its wrong , it will lead to data corruption if the thing was huge .
Last edited on
@Kermot: Yours does not work on ma machine. In fact, it crashed.
closed account (48T7M4Gy)
@shadowCODE Yep, we all know.
This should solve every problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #include <iostream>
 using namespace std;
void stringcpy(const char* src,char* dest){

    while(*src != '\0'){
        *dest++ = *src++;
    }
    *dest = '\0';   //since the '\0' from src is not copied to dest, add it manually to end dest.
//remember that dest is no longer at the beginning but at the end.
}

int main()
{
   const char str[] = "fatima";
  // char str2[] = "";   can hold only one character.
    char str2[sizeof(str)];  //make it hold the same number of characters as the source, including the ending NULL char.

   const char* s  = str;
    char* s2 = str2;

    stringcpy(s, s2);
    cout<<str2<<" ";
    cout<<str;
}

And for reference actual implementation (from libc) of strcpy:
1
2
3
4
5
6
7
char* strcpy(char* dest, const char* src) //Note the order
{
    char* temp = dest;
    while(*dest++ = *src++)
        ;
    return temp;
}
Last edited on

line 8: *dest = '\0';


is there a need ?does the compiler automatically does this …according to me no…asking if maybe i am wrong?
Last edited on
Yes, that code does not copy null teminator so you need to place it manually.
Compiler does not do this automaticly (it actually cannot read minds and know that you want it) and that is why you see garbage when outputting non-terminated string.
Last edited on
Yes there is a need.
There is no string without the ending null character and the compiler will not put it for you in this case.

Without that line, dest will simply be an an array of characters .

e.g

1
2
3
4
5
6

char string[5] = "Bill"; //equivalent to char string[5] = {'B','i','l','l','\0'}, this is a string.
char noString[4] = {'B','i','l','l'};  //simple array of characters as a result of removing line 8

std::cout<<string;  //good
std::cout<<noString //not good. ==> garbage (except overloaded properly) 
Last edited on
Topic archived. No new replies allowed.