views:

131

answers:

5

Hi all , This is my second problem today, pointers are giving me nightmares . I'm trying to make a program that do the same thing that strcpy() function do.. Once i try it..it crashes and i'm 100% sure that's a pointers issue in my code. I think because there is some sort of an unintiallized pointer(*copied) ..But i've assigned NULL to it ...so can anybody tell me what's Null assignment is exactly for ? because i think i misunderstand its use. and tell me please what corrections can be made to the program to run normally .

 #include <iostream>
 using namespace std;

 void mycpy(char *b , char *a);

 int main()
 {

    char *original = "this is a text" ;
    char *copied = 0 ;

    mycpy(copied , original);

    for(int i = 0 ; *(copied+i) ;i++) cout << *(copied+i) ;

    return 0;
}

void mycpy(char *b , char *a){

    for(int i = 0 ; *(a+i) ; i++) *(b+i) = *(a+i);

}

Thanks in advance .

+1  A: 

You have pointers, but they aren't pointing to any memory. You need to allocate some memory for this to work.

char *original;

That is just a pointer to some memory that is of type char. You can't set that to "this is a text" because it is just a pointer. It doesn't have any space to store "this is a text".

char original[ 15 ] = "this is a text";
char copied[ 15 ] = "this is a text";

will work, or

char *original;
char * copied;

original = malloc( 15 );
copied = malloc( 15 );

mycpy( original, "this is a text" );
mycpy( copied, original );

Both of these methods grab 15 bytes for you to store your text. The first example uses 15 bytes from the stack. The second example uses 15 bytes from the heap.

BlueWaldo
+3  A: 

You have to allocate some memory for the result of the copy operation.

In your case copied hasn't been initialised, so you're trying to write to a null pointer. The following will allocate enough memory for you to copy original into copied

char* copied = new char[strlen(original)+1];
Glen
Probably want strlen(original) + 1, and rafael probably wants to copy the terminating \0 as well.
ChrisInEdmonton
@ChrisInEdmonton, thanks, was rushing and not paying attention....
Glen
+3  A: 

Well, your mycpy is almost right (although you could've used brackets instead of arithmetic, i.e. a[i] instead of *(a+1)). In order to print the copied string correctly, the last character must be zero, but the last one is not copied by your function. So it should rather be like

void mycpy(char *b , char *a)
{
    int i;
    for(i = 0 ; *(a+i) ; i++) *(b+i) = *(a+i);
    *(b+i) = 0; // or "\0", doesn't matter    
}

Furthermore the variable

char *copied = 0 ;

doesn't point to valid memory, so you're reading from memory position 0 which is illegal. You could define the variable as array

char copied[20];
AndiDog
A: 

In this line:

for(int i = 0 ; *(a+i) ; i++) *(b+i) = *(a+i);

it's the dereferencing of b+i that causes the error. You're dereferencing 0 (NULL), which is the number 1 pointer error.

Johann Gerell
+1  A: 

Fixed:

 #include <iostream>
 using namespace std;

 void mycpy(char *b , char *a);

 int main()
 {

    char *original = "this is a text" ;
    char copied[30]; // you need to actualy allocate space 
    // (this is on stack, you could use new as well, for heap allocation)

    mycpy(copied , original);

    for(int i = 0 ; *(copied+i) ;i++) cout << *(copied+i) ;

    return 0;
}

void mycpy(char *b , char *a){
    int i =0;
    while (*(a+i)) {
        *(b+i) = *(a+i);
        ++i;
    }
    *(b+i) = '\0'; // null termination
}
rmn