c语言 请问char数组该如何声明?
#include<string.h>#include<stdio.h>voidmain(){chara[]={"abc","defdsf","dsaf"};//char*...
#include <string.h>
#include <stdio.h>
void main()
{
char a[]={"abc","defdsf","dsaf"};
// char *a;
// a=(char *)malloc(100*sizeof(char));
//a[]={"SDF","Hds","sadf"};
char *e[5];
strcpy(e[1],a[1]);
printf("%s",e[1]);
} 展开
#include <stdio.h>
void main()
{
char a[]={"abc","defdsf","dsaf"};
// char *a;
// a=(char *)malloc(100*sizeof(char));
//a[]={"SDF","Hds","sadf"};
char *e[5];
strcpy(e[1],a[1]);
printf("%s",e[1]);
} 展开
展开全部
char a[3][7]={"abc","defdsf","dsaf"};
中括号里的每个字符串,都应该用一个数组来保存了,所以这是一个二维数组。
中括号里的每个字符串,都应该用一个数组来保存了,所以这是一个二维数组。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
int main()
{
int i;
char ch[5] = {'a','b','c','d','\0'};//方式1
char ch2[]="hello";//方式2
char *ch3 = "world";//方式3
char *ch4[3] = {"welcome", "," ,"doudou745"};//字符指针数组
printf("%s\n", ch);
printf("%s\n", ch2);
printf("%s\n", ch3);
for (i = 0; i < 3; i++)
{
printf("ch4[%d] = %s\n", i+1, ch4[i]);
}
return 0;
}
int main()
{
int i;
char ch[5] = {'a','b','c','d','\0'};//方式1
char ch2[]="hello";//方式2
char *ch3 = "world";//方式3
char *ch4[3] = {"welcome", "," ,"doudou745"};//字符指针数组
printf("%s\n", ch);
printf("%s\n", ch2);
printf("%s\n", ch3);
for (i = 0; i < 3; i++)
{
printf("ch4[%d] = %s\n", i+1, ch4[i]);
}
return 0;
}
追问
假如我想將ch4[2]的值strcpy給ch2呢,要怎麼寫呢?
追答
#include
#include
int main()
{
int i;
char ch[5] = {'a','b','c','d','\0'};//方式1
char ch2[]="hello";//方式2
char *ch3 = "world";//方式3
char *ch4[3] = {"welcome", "," ,"doudou745"};//字符指针数组
printf("%s\n", ch);
printf("%s\n", ch2);
printf("%s\n", ch3);
for (i = 0; i < 3; i++)
{
printf("ch4[%d] = %s\n", i+1, ch4[i]);
}
strcpy(ch2,ch4[2]);
printf("%s\n", ch2);
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询