Thread: declaring strings?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    45

    declaring strings?

    I am trying to declare a character string which is big enough to hold 12 variables but only initialized to hold 2.

    I tried doing

    char months [ 12 ] [ 2 ];

    but that didnt work. I could use some help, thanks in advance.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by s_jsstevens View Post
    I am trying to declare a character string which is big enough to hold 12 variables but only initialized to hold 2.
    I don't understand what it is that you're trying to do.

    char months[12] will make a string that can hold 12 chars, including the terminating '\0' char.

    Quote Originally Posted by s_jsstevens View Post
    I tried doing

    char months [ 12 ] [ 2 ];

    but that didnt work. I could use some help, thanks in advance.
    That declares 12 strings of size 2 (or something like that combination).

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    I need to declare a character string array called months which is big enough to hold 12 names, but initialized only to hold the first two names.

    ignore my code i dont know what I am doing, I am new at this

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    So something like this?

    Code:
    char *months[12] = {"Month 1", "Month 2"};
    Initalizing arrays like this will cause the rest of the array to be initalized to 0. That means all entries except the first 2 inside months will be NULL.

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    And also the first two entries are immutable (read-only).

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes and no. The strings themselves are not editable, but you can make the pointers point elsewhere. You also can make them safely point elsewhere without worrying about a memory leak in this particular case, since you're using string literals.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. declaring array of strings dynamically!!!!
    By Real in forum C Programming
    Replies: 1
    Last Post: 08-29-2003, 06:59 AM
  5. am I declaring my strings wrong?
    By sunburnbyRA in forum C++ Programming
    Replies: 7
    Last Post: 03-06-2003, 05:42 PM