1

How should I call this in c programming:

char *fileNames[3] = {"string1", "string2", "string3"};

Pointer array, array pointing to strings, or what?

Not really laziness to look for the answer somewhere else, it is that I didn't find it.

Update 1: I've called the above (whatever it is) fileNames because I'm actually using it to store file names, but it could have been anything in my question.

and Thank you for the huge debate, but there are so many answers that is even difficult to decide which is the right one for me.

Pamplones
  • 85
  • 1
  • 2
  • 7

4 Answers4

6

You generally can read simple C / C++ declarations from right-to-left, but you're supposed to follow the Clockwise/Spiral Rule.

It's an array of pointers to characters.

Matt Cruikshank
  • 2,932
  • 21
  • 24
  • 2
    I think the OP understands what it *is*, (s)he's just asking what to *call* it. In that context, "array of pointers to characters" is a poor name, since it describes its *type* with no information about its *meaning*. (In particular: it ignores the fact that the pointed-to characters are the initial characters of C-style strings.) – ruakh Aug 01 '13 at 17:44
  • 1
    Nonsense. C types are read in a clockwise spiral. See http://c-faq.com/decl/spiral.anderson.html. – nes1983 Aug 01 '13 at 17:47
  • "Pointer array, array pointing to strings, or what?" I don't think you're correct. – Matt Cruikshank Aug 01 '13 at 17:49
  • I'm not correct, I'm just asking and I see it has generated a debate so wasn't as straightforward as I first thought. – Pamplones Aug 01 '13 at 18:12
  • and I have used it in the past but neither understand it very well. – Pamplones Aug 01 '13 at 18:13
  • Pamplones, I was responding to @ruakh, when I meant, "I don't believe you're correct, Pamplones is trying to figure out what to call the type of this, not come up with a name for the variable." – Matt Cruikshank Aug 01 '13 at 18:14
  • @Pamplones, so am I right? Are you trying to understand what to call the type of your fileNames variable, or are you trying to rename your fileNames variable to a more appropriate name? – Matt Cruikshank Aug 01 '13 at 18:23
  • @MattCruikshank: I didn't mean that (s)he was asking for a variable-name, only that (s)he *wasn't* asking for a rote and meaningless characterization of the type. It's true that in C, a "string" and a "pointer to a character" have the same type, but you shouldn't refer to the former as the latter. – ruakh Aug 01 '13 at 23:29
  • I'm trying to understand the type of filenames and also why not use filenames[3] instead? – Pamplones Aug 02 '13 at 05:03
5

The name fileNames is an array of 3 char* (char pointers) that do point to 3 string literals in your declaration. it is be called: 'array of pointers to char' or 'array of strings' because each index points to a string. You should make them constant:

const char *fileNames[3] = {"string1", "string2", "string3"};
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
nio
  • 5,141
  • 2
  • 24
  • 35
  • This is wrong. It's not an array of 3 'char*' pointers. That would be char** []. It's an array of three pointers to a char. – Wiz Aug 01 '13 at 18:08
  • @GrijeshChauhan He wrote "3 char* pointers". That's wrong. That's three pointers to a pointer to a char. – Wiz Aug 01 '13 at 18:09
  • @Wiz yes but I parenthesis Is it correct now? suggest if you can – Grijesh Chauhan Aug 01 '13 at 18:10
  • 1
    @Wiz Thanks! before you I also notice same so I added some more text. and made `pointer` as *`pointer`* – Grijesh Chauhan Aug 01 '13 at 18:15
  • *The name fileNames is an array of 3 char* (char pointers) that do point to 3 string literals* OR more specifically they contain the address of string literals because string literals represent a pointer to themselves . – 0decimal0 Aug 02 '13 at 03:39
4

For this kind of question, there's cdecl. In English, your type is

declare fileNames as array 3 of pointer to char.
nes1983
  • 15,209
  • 4
  • 44
  • 64
0

You can call it as an

array of character pointers

Raju
  • 1,149
  • 1
  • 6
  • 19
  • But why not `Pointer array` **?** or `pointing to strings` **?** its not an answer.-- Not-down-voting. Should improve answer else comment it. – Grijesh Chauhan Aug 01 '13 at 18:40