为啥数组元素不能为字符串
为什么数组元素不能为字符串?
#include "conio.h"
#include <stdio.h>
void main()
{
int numin,temp,a,b,c;
char num[10]={"zero","one","two","three","four",
"five","six","seven","eight","nine"};
printf("please input 3 numbers:");
scanf("%d",&numin);
if (numin>999)
{printf("erro number");}
else
{
a=numin/100;
b=(numin-a*100)/10;
c=((numin-a*100)-b*10);
printf("%s\t%s\t%s\t",num[a],num[b],num[c]);
}
}
编译不过,要改成char* num[10]={"zero","one","two","three","four",
"five","six","seven","eight","nine"};
就可以,这是为什么呢。新手求教育
------解决方案--------------------
因为你的数据类型不对
char num[][10]
或者是
char* num[]
------解决方案--------------------
char num[10];这个表示你定义了一个字符数组,数组长度为10,每个元素就是一个字符,也就是最多可以装下10个字符(包含一个结束符)
而 char *num[10];表示定义个一个字符指针数组,数组的元素是字符指针,而字符指针是可以指向一个字符的...
------解决方案--------------------
你定义的是字符数组char num[],你要定义字符串数组如char num[][],或字符串指针数组char *num[]来存储字符串的地址
#include "conio.h"
#include <stdio.h>
void main()
{
int numin,temp,a,b,c;
char num[10]={"zero","one","two","three","four",
"five","six","seven","eight","nine"};
printf("please input 3 numbers:");
scanf("%d",&numin);
if (numin>999)
{printf("erro number");}
else
{
a=numin/100;
b=(numin-a*100)/10;
c=((numin-a*100)-b*10);
printf("%s\t%s\t%s\t",num[a],num[b],num[c]);
}
}
编译不过,要改成char* num[10]={"zero","one","two","three","four",
"five","six","seven","eight","nine"};
就可以,这是为什么呢。新手求教育
C
指针 数组
------解决方案--------------------
因为你的数据类型不对
char num[][10]
或者是
char* num[]
------解决方案--------------------
char num[10];这个表示你定义了一个字符数组,数组长度为10,每个元素就是一个字符,也就是最多可以装下10个字符(包含一个结束符)
而 char *num[10];表示定义个一个字符指针数组,数组的元素是字符指针,而字符指针是可以指向一个字符的...
------解决方案--------------------
你定义的是字符数组char num[],你要定义字符串数组如char num[][],或字符串指针数组char *num[]来存储字符串的地址