0

How do we create a string array in C? If I want to put a bunch of words in an array, what is the correct way?

char array[] = {'orange', 'banana', 'kiwi', 'apple', 'pineapple'};

Thanks

Samex
  • 13
  • 5
Aamir John
  • 75
  • 2
  • 4
  • 6
  • char arrays in C are also known as strings. A whole word is technically an arrays of chars. You cannot have a whole word be a single char. Eg: char *s = "Hello". – Tdorno Nov 21 '15 at 20:04
  • @Tdorno: A `char` array is **not** a string! Technically, C has no `string` type, but uses a special encoding by convention. And for `char *s = "Hello";`: `s` is a pointer - anoter different type, here it points to a _string literal_. – too honest for this site Nov 21 '15 at 20:05
  • 1
    @Olaf obviously but for someone who has been exposed to string before, it's a better way to think about it, no? – Tdorno Nov 21 '15 at 20:06
  • Perhaps the OP is looking for a 2D character array? – aspiring_sarge Nov 21 '15 at 20:06
  • Possible duplicate of [what is the meaning of char \*s={'h','e','l','l','o','\0'}; in C and how it is different from char \*s="hello";?](http://stackoverflow.com/questions/33688372/what-is-the-meaning-of-char-s-h-e-l-l-o-0-in-c-and-how-it-is-di) – user3386109 Nov 21 '15 at 20:07
  • @Tdorno: Giving a beginner wrong information and/or using wrong terms will just add to his confusion. This is a very bad idea. Simplification - maybe. But please don't **over**simplify. – too honest for this site Nov 21 '15 at 20:08
  • 1
    @Olaf Sorry =D. But... I feel you're somewhat wrong here. A string, by traditional definition reads: a sequence of characters, either as a literal constant or as some kind of variable. Sequential characters is how we represent singular data in C. How is that the wrong way to think about? – Tdorno Nov 21 '15 at 20:11
  • 1
    @AamirJohn Both of your examples will give compiler errors, what did they tell you? – Weather Vane Nov 21 '15 at 20:16
  • 1
    @Tdorno: Please have a look into the standard. C does not have a distinct _character_ type like e.g. the Pascal-languages. `char` is an integer type. In other languages a string is a distingt type, e.g. with a length and arbitrary contents. In C, it is not. So a `char []` is simply an array of integers in the first place. It is just convention to treat the contents special (i.e. process sequentially and stop at a `0` entry - try using a C-string with a `0`-character **inside**). by some functions of the standard library and when initialising with a string literal. – too honest for this site Nov 21 '15 at 20:16
  • Possible duplicate of [Create extern char array in C](http://stackoverflow.com/questions/7670816/create-extern-char-array-in-c) – Rob Nov 21 '15 at 20:41
  • 1
    I don't know why this is spurring such a debate, and receiving a down vote, seeing as the OP clearly is quite new to the field (hence why I have said that the OP should learn C in more comprehensively.) However, marking this question as a waste of time or plain useless is quite invalid, considering I took the time to answer what I thought may help somebody learn–and possibly many others. While technically a string is not a type, the semantic meaning of a string is widely agreed upon in computer science. Also, `char` may only be a one-byte integer type–but in `UNIX` is mapped to the ASCII table – Alexandre Cassagne Nov 23 '15 at 02:18
  • (continued) which is the closest thing to an actual 'character' that can be represented in computer memory. If you believe this question is a waste of time–by all means, don't waste your time teaching semantics to someone who has never written more than a half-dozen lines before. – Alexandre Cassagne Nov 23 '15 at 02:23

2 Answers2

4

It is not entirely clear what you are looking to do, but a string in C is actually a char *, terminated by the NUL character '\0'. In other words, a single char is a character–where a string is an array of char.

As an example, the following are equivalent definitions of strings.

char hello[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char hello[] = "Hello";

Note that in hello[size], in this case size = 6, size needs to be at least the size of the string, including the null terminator '\0'.

As I said previously, it is not completely clear what you are trying to do–if you want to build an array of strings (not the question asked) then I will gladly help you in doing so.

Tutorials on using strings and string.h are vastly available on the web, but I suggest you look for a more comprehensive C course (Harvard's CS50 is a good place to start, and is free).

Good luck,

Alexandre.

Alexandre Cassagne
  • 2,384
  • 23
  • 40
0

Here is a complete C program that declares a string and prints it:

#include<stdio.h>
int main() {

char name[] = "John Q Public";  //declare a string
printf("%s\n", name);           //prints "John Q Public"
}

Declaring strings in C is easy: it's just an array of char.

clay
  • 1,757
  • 2
  • 20
  • 23
  • I almost forgot, if you want to manipulate strings in C, you must either (a) create your own array manipulation functions to edit your char array or (b) include the strings.h library that comes with most C implementation. – clay Nov 21 '15 at 20:30