How to declare an array of strings 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 the elements of an array as output?

The compiler says: "String" is not declared.

+4
source share
3 answers

In this way:

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

A is an array of pointers to char .

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

If you did not enable const , it will work, but the compiler will give you annoying warnings if you do not suppress them with "-w".

+3
source

In C, a string can only be represented as an array of characters. Therefore, to represent an array of strings, you need to create an array (an array of characters). In C ++, we have an STL string, a string, and you can create an array of strings and use it in the form in which you wrote (for example, with modifications of C-specific elements in your code).

+1
source

All Articles