简体   繁体   中英

How to declare string array in c

#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".

This way:

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

A is an array of pointers to char .

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

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM