Click here to Skip to main content
12,397,805 members (54,548 online)
Rate this:
 
Please Sign up or sign in to vote.
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 26-Apr-11 9:34am
Jayfam469
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try something like this:

#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).
  Permalink  
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
Nishant Sivakumar 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
Nishant Sivakumar 26-Apr-11 15:48pm
   
Thanks!
Jayfam 26-Apr-11 15:46pm
   
char* is? i not understand with char*, can you further explain?
Nishant Sivakumar 26-Apr-11 15:49pm
   
A char represents a single character. C does not have a string type. In C, a string is represented by a char*. Pointer to character.
Jayfam 26-Apr-11 15:53pm
   
i got a error of old-style parameter declarations in prototyped function definition
Nishant Sivakumar 26-Apr-11 15:56pm
   
I tried that code in VC++ 2010 and compiled as "C" (not C++). Code compiles fine.
Albert Holguin 26-Apr-11 16:16pm
   
this may be the print() instead of printf() type-o
Espen Harlinn 26-Apr-11 15:58pm
   
Nice reply, 5ed!
Nishant Sivakumar 26-Apr-11 16:03pm
   
Thanks Espen!
Jayfam 26-Apr-11 16:12pm
   
thanks for the help, i finally understand and the problem solve
Nishant Sivakumar 26-Apr-11 16:14pm
   
Glad to hear that, and you are most welcome.
Albert Holguin 26-Apr-11 16:17pm
   
accept a solution to close the question
Jayfam 26-Apr-11 16:18pm
   
accept already
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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).
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

How about:

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!
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 26 Apr 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100