I'm confused!

This is a discussion on I'm confused! within the C++ Programming forums, part of the General Programming Boards category; Hey guys! I hope you can help me sort one thing out. Why i cannot do this? Code: char* ptr ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    30

    I'm confused!

    Hey guys!
    I hope you can help me sort one thing out. Why i cannot do this?
    Code:
    char* ptr = new char[40];
    ptr = "First string";
    strcpy(ptr, "New string");
    However, if i would write this
    Code:
    char* ptr = new char[40];
    strcpy(ptr, "First string");
    strcpy(ptr, "New string");
    then everything would work out and contents of ptr would change to the new string. In other words, how does function strcpy change from normal assignment. Thank you.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    In the first case, you are assigning the address of "First string" into ptr and then you are trying to overwrite it with strcpy() which is a no-no. In the other case, strcpy() uses the pointer as an array and does not change the address contained in the pointer. You could prevent this by having a const pointer to a char like this:
    Code:
    char const* ptr = new char[40];
    ptr = "First string"; // compile-error : cannot change memory address, declared char const*
    strcpy(ptr, "New string");
    Besides, using new[] with a constant size isn't much useful at all. Just to make sure you know.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    char const* ptr = new char[40];
    ptr = "First string"; // compile-error : cannot change memory address, declared char const*
    Actually, no. char const* is the same as const char* so you will get a compile error on the strcpy line. I think you meant:
    Code:
    char *const ptr = new char[40];
    ptr = "First string";//error
    strcpy(ptr,"New string"); //ok
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    30
    So, to make sure i got it - using strcpy is like overwriting existing address with new value. And using operator = is just changing the address to a new value.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Character arrays tend to be rather confusing unless you do simple stuff with them or have a good understanding of pointers, arrays and constants. If you just want to work with text, you may be better off using C++ style "string" objects, which are safe and easy to handle (even if you do have a good understanding of character arrays): http://www.cprogramming.com/tutorial/string.html
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    This is a good read on the subject too: http://pw1.netcom.com/~tjensen/ptr/pointers.htm

    It says C but applies to C++ if you're using char arrays, in case you were wondering.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  2. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 12:13 AM
  3. So Now Im getting confused?!?!?
    By zergdeath1 in forum C++ Programming
    Replies: 11
    Last Post: 03-06-2004, 04:41 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21