注册 登录
编程论坛 C++教室

为什么char型数组不能初始化???大侠帮忙看看

a632034079 发布于 2009-11-24 15:14, 2889 次点击
大侠帮忙瞧瞧为什么这句char s[100]=NULL;初始化不对吗??数组的初始化不就是=NULL的吗???


--------------------Configuration: 统计字符串 - Win32 Debug--------------------
Compiling...
main.cpp
E:\Windos32\统计字符串\main.cpp(7) : error C2440: 'initializing' : cannot convert from 'const int' to 'char [100]'
        There are no conversions to array types, although there are conversions to references or pointers to arrays
执行 cl.exe 时出错.

统计字符串.exe - 1 error(s), 0 warning(s)



#include <iostream.h>
#include <stdio.h>
void main()
{
    cout<<"enter a string:"<<endl;
    int a,b,c,i;
    char s[100]=NULL;
    a=b=c=i=0;
    gets(s);
    for(i=0;i<100;i++)
    {
        if(s[i]=='\0')
            break;
        if(s[i]>='0' && s[i]<='9')
            a++;
        else if(s[i]>='A'&&s[i]<='Z' || s[i]>='a'&&s[i]<='z')
            b++;
        else c++;
    }
    cout<<"SZ="<<a<<endl;
    cout<<"ZF="<<b<<endl;
    cout<<"FH="<<c<<endl;
    getchar();
}
4 回复
#2
flyingcloude2009-11-24 15:33
char s[100]={NULL};

LZ,数组初始化不能像你那样的
#3
a6320340792009-11-24 15:43
以下是引用flyingcloude在2009-11-24 15:33:13的发言:

char s[100]={NULL};

LZ,数组初始化不能像你那样的
去看了下书,发现数要不久不声明要不久都带上了{ }了的.....不好意思,又麻烦版主...........
顺便问下这样对吗?char s[100]={0};
#4
flyingcloude2009-11-24 15:47
回复 3楼 a632034079
对啊
#5
a6320340792009-11-24 15:50
以下是引用flyingcloude在2009-11-24 15:47:43的发言:

对啊
谢谢了
1