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

2
  • 2
    There's no type string in C. Commented Aug 2, 2012 at 9:38
  • Beware that input characters as your ü in "Bülent" may not show up as just one char as you C compiler understands it. Commented Aug 2, 2012 at 11:30

4 Answers 4

22

This way:

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

A is an array of pointers to char.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. Basic structure, but it can not been remembered easily.
6
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".

Comments

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

Comments

0

you can use cs50 library to work with string or you can work with pointers.

CS50

string names[] = {"Mohammed", "Mhammed", "Ali", "Lora"};

pointer

char *names[] = {"Mohammed", "Mhammed", "Ali", "Lora"};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.