Thread: Just curious

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    Just curious

    I was just wondering why it would be necessary to create an array of pointer-to-chars whose elements are assigned to strings. Not only that but a const pointer-to char...I read that in this C++ book I'm using to teach myself and I was just curious why that was necessary....any answers/insight would be great. thanks-Chap
    Code:
    const char* qualify[4] =
    {
        "String 1\n",
        "String 2\n",
        "String 3\n",
        "String 4\n"
    };

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The type of a string literal is array of const char, so to be strictly correct, you must use a pointer to const char to point to a string literal.

    >I was just wondering why it would be necessary to create an array of pointer-to-chars whose elements are assigned to strings.
    It isn't necessary. There are a number of ways to go about having an array of strings, this is just one of them. But the idea is that if the strings don't need to be modified, you can simply assign them to pointers and avoid having to specify an array size.
    My best code is written with the delete key.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Each character array (c-style string) needs a pointer. (A single variable can only hold one character, so you use a single pointer to point-to the array.)

    4 character arrays require 4 pointers. In this case, an array of 4 pointers is used. The program could have used 4 different variables instead of an array (i.e. qualify1 instead of qualify[1]).

    The pointers are constant, so you don't screw them up. You don't want to accidently change one of the pointers. Because of the way arrays are stored in memory, changing the pointers could really foul things up!

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    "String1 \n" is a string literal. It can't be changed. It's value can be assigned to other strings, and if you don't want the other string to change the value either then you need to use the keyword const in front of the keyword char.

    const char * means you have a pointer to a char and the value of the char can't change.

    char * can be a pointer to a single char, or a pointer to the first element of a string of char. You can't tell until it is initialized which it is.

    char * one = "string1 \n"; //one is a string;

    char ch = '+';
    char * one = &ch; //one is a pointer to a single char

    const char * one = "string1"; //one is a string and you can't change the value of the string

    const char one[] = "string1"; //one is a string and you can't change the value of the string

    You can have arrays of arrays, arrays of pointers, pointer to arrays, const variables, const pointers, pointers to const variables, const pointers to const variable, pointers can act like arrays, etc., etc., etc. It's kind of confusing to begin with. What am I saying! It's very confusing to begin with and remains kind of confusing forever--unless you do this kind of thing all the time. But you get used to going back to the reference material time after time, until you get the hang of it.

    In this case the author has declared a array of 4 C style strings, and the value of the strings cannot be changed. He/she could have allowed the values to be changed if they wanted to. They could have used alternate synatx, maybe something like this:
    Code:
    char one[4][20]= 
    {
    	"String 1\n",
    	"String 2\n",
    	"String 3\n",
    }
    (if that's even legal), but chose to use the one they did. I can't say why they chose that syntax over another. It's easy to see why using a vector of STL strings is a commonly used alternative to this type of syntax. Squirming your way through the nuts and bolts at this level can prepare you for understanding what's behind the curtain when you start to use the vectors and STL strings, though. So if you like that approach, continue. If not you can learn about vectors and STL strings first and then look behind the curtain later if you want to.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curious GCC error messages
    By Mario F. in forum Tech Board
    Replies: 1
    Last Post: 11-22-2006, 04:57 PM
  2. Curious about thumdnails
    By Gravedigga in forum Windows Programming
    Replies: 1
    Last Post: 03-08-2004, 10:02 AM
  3. A curious question
    By sbongo in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-02-2004, 09:55 PM
  4. Curious About Master Boot Record
    By civix in forum Tech Board
    Replies: 1
    Last Post: 01-26-2003, 02:19 AM
  5. Another Quick Question....I'm a really curious Person
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 01-12-2002, 01:37 AM