C 获取数组声明错误
我有一个名为C 获取数组声明错误,c,C,我有一个名为arr\u6]的数组,有一个包含六个字符串的想法……但是当我声明这个数组时,编译器会抛出错误 #include <stdio.h> #include <stdlib.h> int main() { int i; char arr_1[]= {"My_name","your Name", "His Name"}; char *arr_p; arr_p = malloc(sizeof(char)*6); arr_
arr\u6]
的数组,有一个包含六个字符串的想法……但是当我声明这个数组时,编译器会抛出错误
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char arr_1[]= {"My_name","your Name", "His Name"};
char *arr_p;
arr_p = malloc(sizeof(char)*6);
arr_p = arr_1;
printf("%s\n",*arr_p);
system("PAUSE");
return 0;
}
请帮帮我 #包括
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
const char *arr_1[]= {"My_name","your Name", "His Name"}; // has to be an array of <char *>
//arr_p is not necessary
printf("%s\n",*arr_1); // will print the first string, "My_name"
printf("%s\n",arr_1[1]); // will print the second string, "your Name"
printf("%s\n",arr_1[2]); // will print the third string, "His Name"
system("PAUSE");
return 0;
}
#包括
int main()
{
int i;
const char*arr_1[]={“我的名字”、“你的名字”、“他的名字”};//必须是
//arr_p不是必需的
printf(“%s\n”,*arr_1);//将打印第一个字符串“My_name”
printf(“%s\n”,arr_1[1]);//将打印第二个字符串“your Name”
printf(“%s\n”,arr_1[2]);//将打印第三个字符串“His Name”
系统(“暂停”);
返回0;
}
我相信您正在寻找的是:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char* arr_1[]= {"My_name","your Name", "His Name", NULL};
char** arr_p;
arr_p = arr_1;
i = 0;
while (arr_p[i] != NULL)
{
printf("%s\n",(arr_p[i]));
++i;
}
system("PAUSE");
return 0;
}
#包括
#包括
int main()
{
int i;
char*arr_1[]={“我的名字”、“你的名字”、“他的名字”,NULL};
字符**arr\p;
arr_p=arr_1;
i=0;
while(arr_p[i]!=NULL)
{
printf(“%s\n”,(arr_p[i]);
++一,;
}
系统(“暂停”);
返回0;
}
以下是我所做的更改列表:
char*arr_1[]
声明字符串数组,因为每个字符串都是字符数组char**
while
循环迭代所有字符串谢谢我的朋友…实际上我只是为了这个…我感谢你的努力…谢谢!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char* arr_1[]= {"My_name","your Name", "His Name", NULL};
char** arr_p;
arr_p = arr_1;
i = 0;
while (arr_p[i] != NULL)
{
printf("%s\n",(arr_p[i]));
++i;
}
system("PAUSE");
return 0;
}