Thread: question about the array in C

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    41

    Post question about the array in C

    I want to declare an array to hold 12 month from Jan to Dec. I am coding like this
    Code:
    char arr[] ={
          "Jan", "Feb",
          "Mar", "Apr",
          "May", "Jun",
          "Jul", "Aug",
          "Sep", "Oct",
          "Nov", "Dec"
    But i got a complaining from the compiler
    Code:
    error: (near initialization for `arr')
    I am not quite to understand. I need some help for this. Thanks

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    char arr[]
    is declaring an array of chars, but
    Code:
    {
          "Jan", "Feb",
          "Mar", "Apr",
          "May", "Jun",
          "Jul", "Aug",
          "Sep", "Oct",
          "Nov", "Dec"
    };
    is assigning an array of const char* to your array.
    You need:
    Code:
    const char* arr[] = {
          "Jan", "Feb",
          "Mar", "Apr",
          "May", "Jun",
          "Jul", "Aug",
          "Sep", "Oct",
          "Nov", "Dec"
    };
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    41
    Thanks for that. To get each month from above array,I use array[i].Is there any other ways to get a month from this array. Can i use
    Code:
    *(arr+i)
    .The compiler gives me an error for this

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well we would need a lot more context/information. For example, if your using exactly that, then yes its an error because theres no semi-colon at the end (though the statement doesnt really "do" anything if its exactly that anyways). Also, what is "i"? We can assume its an integer, if your saying you can do "array[i]", presumably successfully. Are you making sure "i" is less than the length of the array? For your array of months, "i" must be between 0 and 11, inclusive. Also, what is the error message from the compiler? Or you could even post your code.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It SHOULD work:
    Code:
    #include <stdio.h>
    
    int main(void) {
    	int i;
    	char *this[] = { "one", "two", "three" };
    	for (i=0; i<3; i++) printf("%s\n",*(this+i));
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM