Thread: char* functions

  1. #16
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's because you output the addresses of the pointers, not the addresses of the string literals. Try this.
    Code:
    const char* myStringconst()
    {
    	return "Foob";
    }
    
    int main()
    {
    	const void *sconst1 = myStringconst(); 
    	const void *sconst2 = myStringconst();; 
    
    	cout 	<< '\n' << sconst1
    		<< '\n' << sconst2;
    
    	const void *sconst3 = myStringconst();; 
    	const void *sconst4 = myStringconst();;
    	
    	cout 	<< '\n' << sconst3
    		<< '\n' << sconst4
    		<< '\n';
    
    	return 0;
    }
    Oh, and for the fun of it, try this too.
    Code:
    int main()
    {
      const void *p1 = "foo", p2 = "foo", p3 = "foo";
      std::cout << p1 << '\n' << p2 << '\n' << p3 << std::endl;
    }
    Outcome may depend on your compiler options.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #17
    Registered User
    Join Date
    Apr 2007
    Posts
    10
    The bzzzzzt in my head is starting to get clearer now, it’s a wonderful sound! Tanks, again Bee.

    I rest my case…


    Code:
    const char* myStringconst()
    {
    	return "FooBar";
    }
    
    int main()
    {
    
      const void *p1 = myStringconst(), *p2 = myStringconst(), *p3 = myStringconst();
      std::cout << p1 << '\n' << p2 << '\n' << p3 << std::endl;
    
    
      system("pause");
      return 0;
    }
    
    /* OUT: 
    00417A28
    00417A28
    00417A28	
    */

  3. #18
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Noir View Post
    Yes, a string constant has static lifetime.
    The constant itself does, but it's still possible to shoot yourself in the foot. For instance:

    Code:
    #include <iostream>
    
    // This function works correctly.
    const char *get_string()
    {
        char *hello = "Hello!";
        return hello;
    }
    
    // This function is wrong.
    const char *get_string_2()
    {
        // But I'm still using a string constant, right? What's goin' on?
        char hello[] = "Hello!";
        return hello;
    }
    
    // This function makes sure you see the problem
    void kill_stack(int a, int b, int c, int d, int e, int f, int g)
    {
    }
    
    int main()
    {
        const char *str;
    
        str = get_string();
        std::cout << str << std::endl;
        str = get_string_2();
        kill_stack(1,2,3,4,5,6,7);
        std::cout << str << std::endl;
        return 0;
    }
    I'll leave it a mystery for those who don't get it. Arrays and pointers are not the same thing!

  4. #19
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    The constant itself does, but it's still possible to shoot yourself in the foot.
    When you use a string constant to initialize an array, you're not working with a string constant anymore and the lifetime of the array is your rule, not the lifetime of the constant you used to initialize it. It's like you're saying that you can only get hit by a train on train tracks, but it's still possible to shoot yourself in the foot by jumping off a bridge. They're two completely different things.

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Noir View Post
    When you use a string constant to initialize an array, you're not working with a string constant anymore and the lifetime of the array is your rule, not the lifetime of the constant you used to initialize it. It's like you're saying that you can only get hit by a train on train tracks, but it's still possible to shoot yourself in the foot by jumping off a bridge. They're two completely different things.
    The entire point I'm trying to make is that they ARE completely different things, but look alarmingly similar. All I did was replace a "*" with a "[]". This is an immensely confusing aspect of C for many people.

  6. #21
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    The question had nothing to do with the difference between pointers and arrays, so bringing it up doesn't accomplish anything. It's like warning that = is different from == whenever someone posts code using a comparison. It would be good to know...if we cared.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM