GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2009, 17:45
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 23
iblink has a little shameless behaviour in the past
Question

Array initialization error (too many initializers)


Hello. While trying to initialize an array in c, i get this error "too many initializers". I do understand what it means but i need help solving the problem. this is my array below.

CPP / C++ / C Code:
char arrmonths[12] = {"January", "February", "March", "April", "May", "June",         "July","August", "September", "October", "November", "December"};

How do i solve this problem please?
Last edited by admin : 29-Oct-2009 at 03:40. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 28-Oct-2009, 18:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Array initialization error (too many initializers)


Quote:
Originally Posted by iblink
I...need help...[/code]

How do i solve this problem please?
You need an array of pointers to char, not an array of char. Each element of the array of pointers will be initialized to the address of the string literal for that month:

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    /* 
     * An array of twelve pointers to char
     * Each pointer value is set to the address of the string literal
     * corresponding to that month
     */
    char *months[12] = {
        "January", "February", "March", "April", "May", "June",
        "July","August", "September", "October", "November", "December"};

    int i;

    for (i = 0; i < 12; i++) {
        printf("months[%d] = %s\n", i, months[i]);
    }
    return 0;
}

Output
Code:
months[0] = January months[1] = February months[2] = March months[3] = April months[4] = May months[5] = June months[6] = July months[7] = August months[8] = September months[9] = October months[10] = November months[11] = December
  #3  
Old 28-Oct-2009, 18:19
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 23
iblink has a little shameless behaviour in the past

Re: Array initialization error (too many initializers)


Great.. thanks
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 16:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 17:16
Returning a 2 dimensional Array from a function vicky_brsh C++ Forum 1 04-Jan-2008 15:06
What is an array? Howard_L C Programming Language 3 05-Oct-2007 06:11
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 20:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.