Thread: Two questions on string constants

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Two questions on string constants

    Hi

    1) When I have

    Code:
    &"test"
    I get a pointer to the whole array. But since it is an expression, I believe I should get a pointer to pointer to char, i.e. char ** (cf. answer #2 in Question 1.32). Why is it a pointer to the whole array, then?

    2) When I have the initialization

    Code:
    char str[] = "test";
    then str is an array, right?

    Best,
    Niles.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well a pointer to an array (well that one in particular) would be
    Code:
    char (*ptr)[5] = &"test";
    It would be the same with your 2nd example as well
    Code:
    char (*ptr)[5] = &str;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Niels_M
    I get a pointer to the whole array. But since it is an expression, I believe I should get a pointer to pointer to char, i.e. char ** (cf. answer #2 in Question 1.32). Why is it a pointer to the whole array, then?
    Recall what I quoted from the C standard back in your Two-dimensional arrays with strings thread.

    Quote Originally Posted by Niels_M
    then str is an array, right?
    Yes.
    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. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by laserlight View Post
    Recall what I quoted from the C standard back in your Two-dimensional arrays with strings thread.
    Ahh, yes - it is an exception. I should have known that.

    Thanks to both of you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having trouble splitting a main file into 2 classes.
    By lorenzo86 in forum C++ Programming
    Replies: 12
    Last Post: 01-16-2010, 09:46 PM
  2. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  3. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM