strcpy - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

strcpy

tokoGtokoG Posts: 209Member
I was checking the features of [b]strcpy[/b].
This site doesn't mention it but the pointer also should work with [b]strcpy[/b] too. http://www.cplusplus.com/ref/cstring/strcpy.html

The following code doesnt work.

[code]
#include
#include

void main() {
char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* str;


strcpy(str, original);
printf("String pointed by str = '%s'
", str);

fflush(stdin);
getchar();

}
[/code]

So I changed to

[code]
char original[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char str[25];
[/code]

[b]
1. Take off the pointers from both the arrays
2. Added the size of array on both
[/b]

Then it worked. But [b]strcpy[/b] should take the pointer too.
«1

Comments

  • LundinLundin Posts: 3,711Member
    : I was checking the features of [b]strcpy[/b].
    : This site doesn't mention it but the pointer also should work with [b]strcpy[/b] too. http://www.cplusplus.com/ref/cstring/strcpy.html
    :
    : The following code doesnt work.
    :
    : [code]
    : #include
    : #include
    :
    : void main() {
    : char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    : char* str;
    :
    :
    : strcpy(str, original);
    : printf("String pointed by str = '%s'
    ", str);
    :
    : fflush(stdin);
    : getchar();
    :
    : }
    : [/code]


    You haven't allocated any memory for the "str" pointer and it isn't pointing at anything either, so strcpy() will copy it's data to an invalid memory location.

  • bilderbikkelbilderbikkel Posts: 754Member
    [code]
    : void main() {
    [/code]

    [b]main[/b] is of return type int (see www.codepedia.com/1/CppMain for references).
    bilderbikkel

  • tokoGtokoG Posts: 209Member
    [b][red]This message was edited by tokoG at 2006-6-28 22:46:58[/red][/b][hr]
    : : I was checking the features of [b]strcpy[/b].
    : : This site doesn't mention it but the pointer also should work with [b]strcpy[/b] too. http://www.cplusplus.com/ref/cstring/strcpy.html
    : :
    : : The following code doesnt work.
    : :
    : : [code]
    : : #include
    : : #include
    : :
    : : void main() {
    : : char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    : : char* str;
    : :
    : :
    : : strcpy(str, original);
    : : printf("String pointed by str = '%s'
    ", str);
    : :
    : : fflush(stdin);
    : : getchar();
    : :
    : : }
    : : [/code]
    :
    :
    : You haven't allocated any memory for the "str" pointer and it isn't pointing at anything either, so strcpy() will copy it's data to an invalid memory location.
    :
    :

    [blue]
    Hi Lundin

    According to your advice, I modified the code and it worked. :)

    [code]
    void main() {
    char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char* str;

    str = original;

    strcpy(str, original);
    printf("String pointed by str = '%s'
    ", str);

    fflush(stdin);
    getchar();

    }
    [/code]

    Thanks!
    I also needed to try allocating memory [b]dynamically[/b] and this code worked. :)

    [code]
    char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char* str;

    str = (char*)malloc(sizeof(original));
    str = original;

    strcpy(str, original);
    [/code]

    [/blue]
  • tokoGtokoG Posts: 209Member
    : [code]
    : : void main() {
    : [/code]
    :
    : [b]main[/b] is of return type int (see www.codepedia.com/1/CppMain for references).
    : bilderbikkel
    :
    :

    It's provided from my course and some time ago I think because you reminded me of that I asked my course (distance learning) about that and they also said you are right but something like with their policy that they said they will keep using [b]void[/b] for main unless otherwise it returns something specifically.... and I haven't done coding for some time now that I forgot about this! :P


  • Gregry2Gregry2 Posts: 607Member
    : [b][red]This message was edited by tokoG at 2006-6-28 22:46:58[/red][/b][hr]
    : : : I was checking the features of [b]strcpy[/b].
    : : : This site doesn't mention it but the pointer also should work with [b]strcpy[/b] too. http://www.cplusplus.com/ref/cstring/strcpy.html
    : : :
    : : : The following code doesnt work.
    : : :
    : : : [code]
    : : : #include
    : : : #include
    : : :
    : : : void main() {
    : : : char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    : : : char* str;
    : : :
    : : :
    : : : strcpy(str, original);
    : : : printf("String pointed by str = '%s'
    ", str);
    : : :
    : : : fflush(stdin);
    : : : getchar();
    : : :
    : : : }
    : : : [/code]
    : :
    : :
    : : You haven't allocated any memory for the "str" pointer and it isn't pointing at anything either, so strcpy() will copy it's data to an invalid memory location.
    : :
    : :
    :
    : [blue]
    : Hi Lundin
    :
    : According to your advice, I modified the code and it worked. :)
    :
    : [code]
    : void main() {
    : char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    : char* str;
    :
    : str = original;
    :
    : strcpy(str, original);
    : printf("String pointed by str = '%s'
    ", str);
    :
    : fflush(stdin);
    : getchar();
    :
    : }
    : [/code]
    :
    : Thanks!
    : I also needed to try allocating memory [b]dynamically[/b] and this code worked. :)
    :
    : [code]
    : char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    : char* str;
    :
    : str = (char*)malloc(sizeof(original));
    : str = original;
    :
    : strcpy(str, original);
    : [/code]
    :
    : [/blue]
    :

    Take this out

    str=original;

    that makes str point to the data of original, so when you were copying, you were actually copying over the same array...and also, the memory you allocated with malloc, its address is lost, and that memory will be will be hanging on the heap, wasting space until your program ends. Thats the thing about dynamic allocation, you need to keep track of it...you should learn about this later, if you haven't already.

    {2}rIng
  • tokoGtokoG Posts: 209Member
    : Take this out
    :
    : str=original;
    :
    : that makes str point to the data of original, so when you were copying, you were actually copying over the same array...and also, the memory you allocated with malloc, its address is lost, and that memory will be will be hanging on the heap, wasting space until your program ends. Thats the thing about dynamic allocation, you need to keep track of it...you should learn about this later, if you haven't already.
    :
    : {2}rIng
    :


    [blue]
    Hi!

    The thing is,, it worked with [b]str = original[/b] and it doesn't work without... if I take out [b]str = original[/b] as below, it doesnt work. The pop up warning comes up and tells me about the violation. I think I need to add [b]free[/b] function for the memory problem..?

    ps: plus, I was forgetting about using [b]#include [/b] for [b]malloc[/b].

    [code]
    #include
    #include
    #include //malloc

    void main() {
    char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char* str;

    str = (char*)malloc(sizeof(original));
    //[red]TOOK OUT str=original;[/red]
    strcpy(str, original);
    printf("String pointed by str = '%s'
    ", str);

    //[red]ADDED free[/red]
    free(str);
    fflush(stdin);
    getchar();

    }
    [/code]
    [/blue]
  • LundinLundin Posts: 3,711Member
    : : Take this out
    : :
    : : str=original;
    : :
    : : that makes str point to the data of original, so when you were copying, you were actually copying over the same array...and also, the memory you allocated with malloc, its address is lost, and that memory will be will be hanging on the heap, wasting space until your program ends. Thats the thing about dynamic allocation, you need to keep track of it...you should learn about this later, if you haven't already.
    : :
    : : {2}rIng
    : :
    :
    :
    : [blue]
    : Hi!
    :
    : The thing is,, it worked with [b]str = original[/b] and it doesn't work without... if I take out [b]str = original[/b] as below, it doesnt work. The pop up warning comes up and tells me about the violation. I think I need to add [b]free[/b] function for the memory problem..?
    :
    : ps: plus, I was forgetting about using [b]#include [/b] for [b]malloc[/b].
    :
    : [code]
    : #include
    : #include
    : #include //malloc
    :
    : void main() {
    : char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    : char* str;
    :
    : str = (char*)malloc(sizeof(original));
    : //[red]TOOK OUT str=original;[/red]
    : strcpy(str, original);
    : printf("String pointed by str = '%s'
    ", str);
    :
    : //[red]ADDED free[/red]
    : free(str);
    : fflush(stdin);
    : getchar();
    :
    : }
    : [/code]
    : [/blue]
    :


    str = (char*)malloc(sizeof(original));
    strcpy(str, original);

    No, this is also wrong. Because you made original a pointer and not an array. The difference:

    char[] original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    Here you tell the compiler to allocate space for an array large enough to contain the whole string + the null termination. It will be allocated at address "original" in memory, ie if you type "original" you will get a pointer pointing at the data.


    char* original = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    An array will be allocated here as well, but it will be allocated on the stack in some way defined by the compiler and after that the pointer variable "original" is set to point at that address. So in this case "original" is a -pointer- not an -array-!

    Confusing? Check this: http://c-faq.com/aryptr/index.html
    and this in particular: http://c-faq.com/aryptr/aryptr2.html

    Since it is a pointer, you can let it point somewhere else whenever you wish, which you can't in the first case.

    In the first case, the sizeof() operator would have returned the correct size and your code would be fine. But in the second case you pass a pointer to sizeof() and get the size of a pointer, which is 4 bytes on a 32-bit OS. So when you call strcpy() there is only room for the first 4 bytes and the rest gets written out of bounds.
  • stoberstober Posts: 9,765Member ✭✭✭
    : It's provided from my course and some time ago I think because you reminded me of that I asked my course (distance learning) about that and they also said you are right but something like with their policy that they said they will keep using [b]void[/b] for main unless otherwise it returns something specifically.... and I haven't done coding for some time now that I forgot about this! :P
    :
    :
    :

    Drop that course immediately! If they insist on using [b]void[/b] main then they are just wasting bandwidth with that course. God only knows that other crappy advice they are feeding you. You will be better off buying a new book and learning from it or take a course at a local junior college.
  • LundinLundin Posts: 3,711Member
    : : It's provided from my course and some time ago I think because you reminded me of that I asked my course (distance learning) about that and they also said you are right but something like with their policy that they said they will keep using [b]void[/b] for main unless otherwise it returns something specifically.... and I haven't done coding for some time now that I forgot about this! :P
    : :
    : :
    : :
    :
    : Drop that course immediately! If they insist on using [b]void[/b] main then they are just wasting bandwidth with that course. God only knows that other crappy advice they are feeding you. You will be better off buying a new book and learning from it or take a course at a local junior college.
    :


    That's nothing. I once had a teacher encouraging us to write as unreadable and complicated code as possible so that it would be impossible for someone else to maintain it...
    Same teacher gave me a high grade for writing a program that was able to display an ASCII-value on the screen corresponding to a key press. He had searched everywhere for such a remarkable program...
  • IDKIDK Posts: 1,784Member
    : : : It's provided from my course and some time ago I think because you reminded me of that I asked my course (distance learning) about that and they also said you are right but something like with their policy that they said they will keep using [b]void[/b] for main unless otherwise it returns something specifically.... and I haven't done coding for some time now that I forgot about this! :P
    : : :
    : : :
    : : :
    : :
    : : Drop that course immediately! If they insist on using [b]void[/b] main then they are just wasting bandwidth with that course. God only knows that other crappy advice they are feeding you. You will be better off buying a new book and learning from it or take a course at a local junior college.
    : :
    :
    :
    : That's nothing. I once had a teacher encouraging us to write as unreadable and complicated code as possible so that it would be impossible for someone else to maintain it...
    : Same teacher gave me a high grade for writing a program that was able to display an ASCII-value on the screen corresponding to a key press. He had searched everywhere for such a remarkable program...
    :
    LOL!
«1
Sign In or Register to comment.