如何在c中声明字符串数组

时间:2021-05-10 21:30:08
#include<stdio.h>

int main()
{
int i;
string A[]={"Ahmet", "Mehmet", "Bulent", "Fuat"};

for(i=0;i<=3;i++){
printf("%s",A[i]);
}
return 0;
}

How can i see my array's elements as output?

我怎样才能看到我的数组元素作为输出?

Compiler says "'string' undeclared".

编译器说“'字符串'未声明”。

3 个解决方案

#1


13  

This way:

 char *A[] = {"Ahmet", "Mehmet", "Bülent", "Fuat"};

A is an array of pointers to char.

A是char的指针数组。

#2


4  

const char *A[] = {"Ahmet", "Mehmet", "Bülent", "Fuat"};

If you don't include the const, it will work but the compiler will give you annoying warnings unless you suppress them with "-w".

如果你不包含const,它会工作,但编译器会给你恼人的警告,除非你用“-w”来抑制它们。

#3


1  

In C, a string can only be represented, as an array of characters.So, to represent an array of strings you have to make array of (array of characters). In C++ we have a STL called, string and you can make an array of string and use it in the the way you have written(ofcourse with modifications to C specific stuff in your code).

在C中,一个字符串只能表示为一个字符数组。因此,要表示一个字符串数组,你必须使用数组(字符数组)。在C ++中,我们有一个名为string的STL,您可以创建一个字符串数组,并按照您编写的方式使用它(当然,在您的代码中修改C特定的东西)。

#1


13  

This way:

 char *A[] = {"Ahmet", "Mehmet", "Bülent", "Fuat"};

A is an array of pointers to char.

A是char的指针数组。

#2


4  

const char *A[] = {"Ahmet", "Mehmet", "Bülent", "Fuat"};

If you don't include the const, it will work but the compiler will give you annoying warnings unless you suppress them with "-w".

如果你不包含const,它会工作,但编译器会给你恼人的警告,除非你用“-w”来抑制它们。

#3


1  

In C, a string can only be represented, as an array of characters.So, to represent an array of strings you have to make array of (array of characters). In C++ we have a STL called, string and you can make an array of string and use it in the the way you have written(ofcourse with modifications to C specific stuff in your code).

在C中,一个字符串只能表示为一个字符数组。因此,要表示一个字符串数组,你必须使用数组(字符数组)。在C ++中,我们有一个名为string的STL,您可以创建一个字符串数组,并按照您编写的方式使用它(当然,在您的代码中修改C特定的东西)。