strcpy

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.

Comments

  • : 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.

  • [code]
    : void main() {
    [/code]

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

  • [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]
  • : [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


  • : [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
  • : 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]
  • : : 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.
  • : 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.
  • : : 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...
  • : : : 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!
  • [b][red]This message was edited by Gregry2 at 2006-6-29 17:59:10[/red][/b][hr]
    : : : : 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!
    :

    lol! Yeah, but I've actually seen a webpage giving all sort of tips on how to write unmanagable code, saying that it was to throw off those code maintainers, to keep your own job...its rather funny. Here http://mindprod.com/unmain.html ...lookee, especially at the naming section :-)

    To tokoG: You don't have actually to change courses and waste money, the guys are just joking...I think...but be careful what they tell you. If anything seems weird or doesn't work, tell us about it and we'll help :-)

    If I recall, this is sort of a side course, your major is something else, right?
    {2}rIng



  • : To tokoG: You don't have actually to change courses and waste money, the guys are just joking...I think...but be careful what they tell you. If anything seems weird or doesn't work, tell us about it and we'll help :-)
    :
    : If I recall, this is sort of a side course, your major is something else, right?
    : {2}rIng
    :
    :
    :


    :) Yup yup yup, my major is something else not the programming.
    I am thinking the reason why I needed to take programming is to make us get used to with the 'programming' or 'technical designing' or 'mathmatical' idea / notation of it. I understand it completely and programming notation is something I've never been through in my entire life!! :D

    Even to communicate with you guys here is entirely new thing for me and I do enjoy it! Again sorry for my lack of knowledge on programming as I just started it last year and sadly.. (or more releave? lol) I am finishing it on October and I am having a final exam on August 10th!!! (ahhgggggrrrr!)

    It's also interesting to know or realise the each person has the programming style. I mean, sure of course but if you never deal with programming you would have no idea what the world of programming is!

    What a nerdy teacher to teach and promote unreadabe code!!
    Proberbly not practical but interesting...

    Well, I will stick to something readable and comprehensible as that's only what I can stick to... to be nerdy requires great amount of skills.


  • :
    : :) Yup yup yup, my major is something else not the programming.
    : I am thinking the reason why I needed to take programming is to make us get used to with the 'programming' or 'technical designing' or 'mathmatical' idea / notation of it. I understand it completely and programming notation is something I've never been through in my entire life!! :D
    :
    : Even to communicate with you guys here is entirely new thing for me and I do enjoy it! Again sorry for my lack of knowledge on programming as I just started it last year and sadly.. (or more releave? lol) I am finishing it on October and I am having a final exam on August 10th!!! (ahhgggggrrrr!)
    :

    Its sad, no relief...:-(...but we're all flattered, I'm sure, that we had such an effect on you. Thanx for the comment. If some of us aren't, the left side of their brain has already devoured their right, they have no more emotion...they are...no longer human:-o...

    You know, my mom was studying to become a nurse, and she had to take calculus...not that I don't like math, as most programmers love it, but the practical use for calculus in the nursing field...um...

    What was ur major anyway?

    : It's also interesting to know or realise the each person has the programming style. I mean, sure of course but if you never deal with programming you would have no idea what the world of programming is!
    :
    : What a nerdy teacher to teach and promote unreadabe code!!
    : Proberbly not practical but interesting...
    :
    : Well, I will stick to something readable and comprehensible as that's only what I can stick to... to be nerdy requires great amount of skills.
    :

    If you read it the other way, it says that people who have a great amount of skills are nerds. Hehe, the gurus here apply!
    {2}rIng
  • : :
    : : :) Yup yup yup, my major is something else not the programming.
    : : I am thinking the reason why I needed to take programming is to make us get used to with the 'programming' or 'technical designing' or 'mathmatical' idea / notation of it. I understand it completely and programming notation is something I've never been through in my entire life!! :D
    : :
    : : Even to communicate with you guys here is entirely new thing for me and I do enjoy it! Again sorry for my lack of knowledge on programming as I just started it last year and sadly.. (or more releave? lol) I am finishing it on October and I am having a final exam on August 10th!!! (ahhgggggrrrr!)
    : :
    :
    : Its sad, no relief...:-(...but we're all flattered, I'm sure, that we had such an effect on you. Thanx for the comment. If some of us aren't, the left side of their brain has already devoured their right, they have no more emotion...they are...no longer human:-o...
    :
    : You know, my mom was studying to become a nurse, and she had to take calculus...not that I don't like math, as most programmers love it, but the practical use for calculus in the nursing field...um...
    :
    : What was ur major anyway?

    [blue]
    Yes, it's bit sad... because I started understanding a little weee bit of programming world and starting knowing you guys. Knowing or learning comletely new concept is always a challenging but fun. Then, it started getting really head (difficult) so it's a little bit of relief too but I will check this web site time to time not to forget about the subjects. I think the more and more nre ppl logong this page and start asking questions which I asked before! Well, then I can asnwer!

    My friend studied Business and she did computer programming as well. I think in the many field the need of programming or at least let's say the skill of understanding the programming notation is much more required.

    I am studying Product Design Technology. So yes I am gonna go thru calculus too. Scary!! I know the ppl do programming must be good at math. I am not and there will be more and more ppl involved in programming courses without even the basic knowledge of math and for them or for me, the simple notation of programming IS already a math notation and therefore is quite difficult.
    [/blue]


    : : It's also interesting to know or realise the each person has the programming style. I mean, sure of course but if you never deal with programming you would have no idea what the world of programming is!
    : :
    : : What a nerdy teacher to teach and promote unreadabe code!!
    : : Proberbly not practical but interesting...
    : :
    : : Well, I will stick to something readable and comprehensible as that's only what I can stick to... to be nerdy requires great amount of skills.
    : :
    :
    : If you read it the other way, it says that people who have a great amount of skills are nerds. Hehe, the gurus here apply!
    : {2}rIng
    :
    [blue]
    Oh I think yes deffinitly. :D
    ANY subjects ANY nerds must be more or less smarter than other ppl who are not nerdy. I am from Japan and we use the word, Otaku for nerds. It started as a negative meaning calling somebody Otaku is like someone who never gets followed by girls or boys (for girl Otaku) but nowadays I think Otaku are getting citizenship. Ppl started realising how valuable their skills and knowledges are. Innovative at the same time sad part of it and it's truth I think is that non-nerdy ppl try to make money out of nerd's idea. Look at open sourse firefox and all that, it all came from nerds or ppl with less or no money. Ideas do not come from riches because they dont need to do "something" about it. But it's always the richs who make business out of it to make them selves richer because they have capital and can start anything. Parasites.

    But the ideas always emerge from ordinary ppl or nerdy ppl. :)
    I am bit nerdy on certain subjects (not computer or programming though) too.
    [/blue]
Sign In or Register to comment.

Howdy, Stranger!

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

Categories