**WHY StrCpy - dosen't work normally :( ? Where is mistakes?**

#include"string.c"

int main()
{

    printf("Hello World!\n");

    char *text = "GOOOD TEXT ALL TIME VERY POOR ";
    int temp = strlen(text);
    char *copy_text;
    printf("size text: %d\n", temp);
    StrCpy( copy_text, text );
    printf("TEXT: %s\n COPY_TEXT %s", text, copy_text );


    return 0;
}




//string.c

int StrLen( const char* str )
{
    if( str != ZERO )
    {
        const char* copy_str;

        for( copy_str = str; *copy_str; ++copy_str )
        {
            ;/* Discard */
        }
        return ( copy_str - str );
    }
    else
    {
        return ZERO;
    }
}

char* StrCpy( char* destination, const char* source )
{
    int size_source = StrLen( source );
    int size_destination = StrLen( destination ); //     !!!! Every time zero

    if( size_source )
    {
        memcpy( destination, source, ( size_source + 1 ) );
        return destination;
    }
    else
    {
        return ZERO;
    }
} 
// 

Recommended Answers

All 4 Replies

You've encountered a common beginner issue. Allow me to point it out more clearly:

// This is a pointer to a string literal. You can't modify it.
char *text = "GOOOD TEXT ALL TIME VERY POOR ";

// This is an uninitialized pointer, you can't read from or write to
// it until it points to a valid block of memory that you own.
char *copy_text;

// This is undefined behavior because you're trying to write
// to an uninitialized pointer. If you're lucky, the program
// will crash fantastically so that the error is obvious, but
// there's no guarantee of that.
StrCpy( copy_text, text );

Now let's fix it:

char *text = "GOOOD TEXT ALL TIME VERY POOR ";
char copy_text[50];
StrCpy( copy_text, text );

In the above correction, copy_text is an array. This means you own the memory, and it's large enough to hold all of the characters in text.

Now, I understood. Thank you! And I have one more question. When I

char *text = " It is dangerous!!!"
int temp = StrLen( text );
char copy_text[ temp ];

StrCpy( copy_text, text );

//I got size of - destination only six ?? :( Where is all size?

Works for me, once I fix the error of trying to define an array with a non-const size (my compiler doesn't support C99+).

Since I can't reproduce the stated problem, you'll need to follow debugging practices such as tracing through the code in a debugger and watching the size that's calculated. Presumably the issue is in StrLen, and that's where I'd start after confirming exactly where 6 originates in your main function.

Works fine for me using mingw 4.5.2 which is a c99 compiler. The only thing I would say is that the third line of StrCpy

    int size_destination = StrLen( destination ); //     !!!! Every time zero

is dangerous because you have no guarantee at all that the destination pointer currently points to a valid string which means you could end up scanning all of physical memory until you find a '\0' or cause a program crash. Strictly it is undefined behaviour because you may end up accessing an object outside its bounds.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.