strings to pointers

This is a discussion on strings to pointers within the C++ Programming forums, part of the General Programming Boards category; I have setup a new class and in that new class I have a private pointer. I need to make ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    26

    strings to pointers

    I have setup a new class and in that new class I have a private pointer. I need to make that private pointer point to a string, let's say "Good Luck". My compiler doesn't allow me to code my private pointer like this

    char *message = "Good Luck";

    So I use

    char *message;

    instead. Then in my constructor I am trying to set my private pointer, called message, to "Good Luck" and have not been able to get it to work.

    Is this possible? If so can anybody help.

    Thanks,

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Code:
    class Test
         {
         public:
              Test() : message("Good Luck")
                   {
                   std::cout << message << std::endl;
                   }
         private:
              const char
                   *message;
         };
    
    int main(void)
         {
         Test test;
         return 0;
         }
    Putting it on the constructor like that is what we call the initializer list.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    26
    That worked.

    Thank you

  4. #4
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Do you understand it ?
    [code]

    your code here....

    [/code]

  5. #5
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Out of curiousity, where is the string "Good Luck" stored? Does it last as long as the object exists?

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    it is const, much the same way as if you did a global:

    char *GlobalString = "Hello There";
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    So just to confirm, do all string literals last throughout the lifetime of a program?

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    I thought you had to use addresses, like:
    Code:
    char* stringPtr;
    stringPtr = &"Hello There";
    or
    Code:
    char hThere[12] = "Hello There";
    char* stringPtr;
    stringPtr = &hThere;
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Originally posted by Dante Shamest
    So just to confirm, do all string literals last throughout the lifetime of a program?
    hardcoded things are basically part of the program and thus are there for the entire time the code is loaded.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    All string literals last throughout the lifetime of the program. Furthermore duplicates may or may not be "folded" such that only one copy of the string exists. In addition they generally live in a section of memory known in technical circles as the "fiddly bits" such that writing to them invokes dark and mystirous forces. Frequently they live in the code/text segment, same as the instructions, unless you would find this desireable for any reason, whereupon the compiler will figure out a way to make them live on the stack.

    The relationship between
    char *s1 = "Hello";
    char s2[] = "Hello";
    char *s3 = s2;
    can be confusing. s2 looks like an address of elements of type char. That is, it is a value sutable for a pointer of type char*. Much the same way as 7 is a value suitable for assignment to an int. Same deal with "Hello" it looks like an address to your program. &s2 is the location where s2 lives, it's just redundant and is a value of type address of chars. &s1 is a totally different beast and is the address where the pointer s1 lives and is a value of type pointer to char. There is no s2 pointer, it's a symbol for your convenence. There may or may not be another location in your program that contains the "Hello" literal information that is copied to the six chars at location s2. &"Hello" makes my head hurt but my guess is that it is the same deal as &s2.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Pointers With Strings
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 02-14-2006, 06:28 AM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. pointers to strings
    By LiLgirL in forum C Programming
    Replies: 3
    Last Post: 04-23-2004, 03:38 PM
  5. More on pointers and strings
    By mart_man00 in forum C Programming
    Replies: 4
    Last Post: 02-10-2003, 06:10 PM

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