Thread: const char* clarification

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    so by that reasoning surely i could of just said p_new_element->city = my_str;
    That would be syntactically correct, but still wrong: my_str is a local array, whereas the node must outlive the function call, so you would end up with a pointer to something that no longer exists. Hence in your case you need to do copying, not aliasing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    so copying the string to the location allocated by the malloc call was correct?
    incidently could i equally of used memove (a term i came across today in my reading and haven't looked up yet)

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. Notice that in post #1, the strings are string literals: so lifetime is not an issue. You might use aliasing to make it more convenient to refer to a string, e.g., if you find yourself referring to p_new_element->city often, maybe you'll write:
    Code:
    char *city = p_new_element->city;
    Then just use city, and this would work even if p_new_element->city was an array of char instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    Quote Originally Posted by cooper1200 View Post
    and as i said to catacombs whom im sure you will agree understands more and is a better programmer than me
    I'm learning C just like you, homie.

  5. #20
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    Quote Originally Posted by laserlight View Post
    It depends on what do you want: aliasing or copying?
    I thought copying, but that can only be done, it seems, with strcpy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  3. Difference between const char * and char const *
    By explorecpp in forum C Programming
    Replies: 4
    Last Post: 08-09-2008, 04:48 AM
  4. Replies: 7
    Last Post: 04-28-2008, 09:20 AM
  5. Assigning Const Char*s, Char*s, and Char[]s to wach other
    By Inquirer in forum Linux Programming
    Replies: 1
    Last Post: 04-29-2003, 10:52 PM

Tags for this Thread