关于C的声明问题 [英] Declaration problem on C

查看:95
本文介绍了关于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?

推荐答案

尝试以下操作:

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));
}



请注意我是如何创建char*数组(而不是char数组)的.



Note how I''ve created an array of char* (and not an array of char).


这是错误的,因为您声明了个字符的数组,并尝试使用(常量)字符指针 的数组对其进行初始化.正确的代码是:
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"};



(您实际上不需要定义MONTHS).



(you don''t really need to define MONTHS).


如何:

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.简单来说,字符串是指向char的指针(好吧,我知道这简化了它,但是这个想法大体上是正确的)-因此,您需要正确声明"m".

2.数组是从零开始的-因此要使用索引1到12,需要将第零个元素用作虚拟对象.

3.您的格式字符串需要反映出您试图打印一个整数和一个字符串而不是2个字符的事实.

4.您不能只打印"m"-您需要指定数组的元素.

5.格式字符串中的换行符将使您的输出更具可读性-它们必须是显式的,而不是隐含的.

6.您可能会有用地获得Kernighan&的副本.里奇( http://en.wikipedia.org/wiki/The_C_Programming_Language [ http://msdn.microsoft.com/zh-CN/library/ms647466(VS.85).aspx [



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!


这篇关于关于C的声明问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆