tags:

views:

254

answers:

4

Is this from the C standard?

+3  A: 

A pointer to an array of 12 characters.

Matthew Flaschen
`char` means byte, not character.
dan04
@dan, if you're going to be pedantic, you should also be right. "An object declared as type char is large enough to store any member of the basic execution character set." (§6.2.5 of C99) A `char` can't hold (e.g.) an arbitrary Unicode character, but it can hold basic characters as defined by C.
Matthew Flaschen
@dan04: Considering `char` automatically converts its value into the corresponding ASCII character, I'm pretty sure that qualifies it as a character at least in some sense.
Maulrus
@Maulrus: There really isn't any ASCII conversion. You might be confused by the 'x' syntax, but 'x' is just a literal integer value of type int. Conversion of the 'x' from ASCII or any other encoding the file happens to have occurs at compilation/translation time.
Peaker
`char` is just a small integer type. `int8_t` in most implementations. It's often used for ASCII characters (or nowadays, UTF-8 code units) but doesn't have to be. A `char` array could even represent pixels in a bitmap.
dan04
@dan, I didn't say otherwise. Yes, `char` holds numbers, but the standard says, "The three types char, signed char, and unsigned char are collectively called the character types." so I still think your distinction is both pedantic and incorrect.
Matthew Flaschen
@Matthew: How many characters are in the string "¿Cómo estás?"?
dan04
C defines character as either "a member of a set of elements used for the organization, control, or representation of data" or a "single-byte character: bit representation that fits in a byte". Your question doesn't say which you mean. If the former, you don't say which set we're dealing with. If the latter, you don't say the encoding. So your question doesn't make sense.
Matthew Flaschen
multibyte encodings made all this a possible debate! "¿Cómo estás?" contains 11 characters for a human (do not count the space, it is not a character for a human), or indeed 9, not counting punctuation marks. In ISO 8859 encodings, C counts 12 char, which is correct (space included). If the string is UTF-8 encoded or any other non-single byte encoding, the count is "of course" wrong. Nonetheless, that "wrong count" is always made of char(acter)s from the C point of view.
ShinTakezou
Fair enough, my understanding of it was obviously wrong. Thanks for the clarification peaker!
Maulrus
+1  A: 

a is a pointer pointing to an array of 12 characters.

Vijay Sarathi
`a` is not a character pointer.
Didier Trosset
@didier then what kind of pointer is it...if i am wrong
Vijay Sarathi
It is a **pointer**. You could say an *array* pointer, but it is useless to add an adjective to pointer when you add *pointing to* afterwards. Writting *character pointer* is misleading.
Didier Trosset
Your answer is wrong, Benjamin, and shows a misunderstanding of pointers. Please fix it, or better yet delete it since correct answers were already given. First of all, just like in `int a`, here `a` is a variable. What type has this variable? "a pointer to an array of 12 elements". Just like in `char* a`, `a` is "a pointer to a char". Second, you can't make one pointer point to another kind of data without casting (well, in C you can with `void*` only, but this is off the point)
Eli Bendersky
+15  A: 

If you're confused by a C declaration, you can use the cdecl program to explain it:

~$ cdecl
Type `help' or `?' for help
cdecl> explain char (*a)[12];
declare a as pointer to array 12 of char
dan04
cool, I did not know it! (I have my notebook with a small list of strange decls I tend to forget) +1
ShinTakezou
I did not know this one, thanks!
Dpp
+2  A: 

Because declarations in C follow the operator precedence rules (ie array subscription is evaluated before indirection), you'll need parens to declare pointers to array types.

In many use cases, there's not really any practical benefit over using a plain char *, except that it's a way to enforce the array size, especially when used as a function parameter:

void foo(char bar[42]);

is equivalent to

void foo(char *bar);

and accepts any char *, whereas

void foo(char (*bar)[42]);

will only accept pointers to arrays of size 42.

As accessing the elements of bar in the latter case is cumbersome, it might be a good idea to immediately define an equivalent char * in the function body

char *baz = *bar;

so that you can use direct subscription baz[13] instead of (*bar)[13].

Christoph