Jens Thoms Toerring wrote:
rahul <ra*********@gmail.comwrote:
>On Jun 10, 11:11 am, Vijay <k.anura...@yahoo.comwrote:
>>Hello everybody, I am new to the C language can anybody tell me the
diffrence between
char nm[10] and char *nm ?
>char foo[10] defines an array of 10 characters. char *bar defines bar
as a pointer to a character.
>Array name without any indexes is the pointer to the first element of
the array.
Not really, an unadored array name stands for (not is) a pointer
to the first element of the array in a context where a value is
required.
That's not the rule.
The implicit conversion is specified
as taking place always, with three exceptions.
For example in
int a[10];
int *b = malloc( 100 );
a = b;
'a' is not a pointer.
(a) is converted to a pointer type in that context.
First reason is that 'a' appears not in
a position where a value is allowed. Here you need something
you can assign a value to. Second, if 'a' would be a pointer
the assignment would be correct (you can assign a new value
to a pointer) and you could actually change 'a' by assigning
it a new value and suddenly '*a' would be the first value in
the memory 'b' points to while 'a[0]' would still be the value
of the first element of the array.
That's wrong.
The standard explicitly states that an array type is not a
"modifiable lvalue", however, even if it didn't:
The conversion of the array type expression to a pointer type,
makes the resulting expression not an lvalue,
which is sufficient to disallow an array type from appearing
as a left assignment operand.
6.3.2 Other operands
6.3.2.1 Lvalues and function designators
[#3] Except when it is the operand of the sizeof operator or
the unary & operator, or is a string literal used to
initialize an array, an expression that has type ``array of
type'' is converted to an expression with type ``pointer to
type'' that points to the initial element of the array
object and is not an lvalue.
6.5.16 Assignment operators
[#2] An assignment operator shall have a modifiable lvalue
as its left operand.
--
pete