Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
C#
#include<stdio.h>
#define MONTHS 13
int main(void)
{
    char m[MONTHS] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int i;
    print("%s%13s", "Input", "output");
    for(i=1 ; i<MONTHS ; i++){
        printf("%7c%13c", i, m);
    }
    return 0;
}


This is my coding and may i know is it the declaration of the variable char wrong?
Posted

Try something like this:

C++
#define MONTHS 12

char* m[MONTHS] =
{
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};

int i;

for(i=0 ; i<MONTHS ; i++)
{
    printf("%d: %s\n", i + 1, *(m + i));
}


Note how I've created an array of char* (and not an array of char).
 
Share this answer
 
Comments
Jayfam 26-Apr-11 15:43pm    
can u modify my coding made it works, because i hope can using my coding rather copy other people coding
Nish Nishant 26-Apr-11 15:44pm    
I just changed your existing code. I did re-format it, that's all. But it is indeed your own code :-)
Albert Holguin 26-Apr-11 16:14pm    
people here help you identify the problem, its up to you to fix your own code, don't abuse the help others are providing
Tarakeshwar Reddy 26-Apr-11 15:46pm    
My 5
Nish Nishant 26-Apr-11 15:48pm    
Thanks!
It is wrong because you declare an array of characters and try to initialize it with an array of (const) character pointers. The right code would be:
const char * m[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};


(you don't really need to define MONTHS).
 
Share this answer
 
Comments
Albert Holguin 26-Apr-11 16:13pm    
my 5
CPallini 26-Apr-11 16:37pm    
Thank you.
Jayfam 26-Apr-11 16:16pm    
thanks
CPallini 26-Apr-11 16:37pm    
You are welcome.
How about:

C#
char *m[MONTHS] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int i;
    printf("%s%13s\n", "Input", "output");
    for(i=1 ; i<xMONTHS ; i++){
        printf("%7d%13s\n", i, m[i]);
    }


1. In simple terms a string is a pointer to a char (OK I know that oversimplifies it, but the idea's roughly right) - so you need to get your declaration of 'm' right.

2. Arrays are zero based - so to use indices 1 to 12, you need the zero'th element as a dummy.

3. Your format strings need to reflect the fact that you are trying to print an integer and a string - not 2 chars.

4. You can't just print 'm' - you need to specify the element of the array.

5. New line characters in the format strings will make your output more readable - they need to be explicit and are not implied.

6. You might usefully get hold of a copy of Kernighan & Ritchie (http://en.wikipedia.org/wiki/The_C_Programming_Language[^]) which (at least when I got it) was the Bible for learning C (and any C-like) language.

7. Depending on your development environment you might also consider ditching functions like printf for the string safe counterparts like StringCchPrintf (http://msdn.microsoft.com/en-us/library/ms647466(VS.85).aspx[^]) that offer some protection against ubiquitous buffer overrun bugs beloved of hackers!
 
Share this answer
 
Comments
Jayfam 26-Apr-11 16:12pm    
thanks for the help, i finally understand and the problem solve

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900