array questions

J

junky_fellow

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

Also, why arr is called a non modifiable lvalue ?

why arr++ not allowed but arr+1 is allowed ?
 
M

Mehta Shailendrakumar

For the second question,
probably arr++ assigns arr a new value and (arr+1) is used only for
dereferencing and the expression itself doesn't change arr's value.
 
T

Them

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

arr is the address of the first element, arr[0]. arr + 1 is address of
second element, arr[1], and so on. What is anomalous about this?
Also, why arr is called a non modifiable lvalue ?

why arr++ not allowed but arr+1 is allowed ?

arr++ is modifying the lvalue (arr = arr + 1), whereas arr + 1 is an
rvalue (returns a value).
 
P

pete

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

The name of an array is always converted to a pointer
to it's first element, except in three cases.

N869
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.
Also, why arr is called a non modifiable lvalue ?

Because any attempt to modify an array type expression
results in a situation where the array type expression
is first converted to an rlvalue.
why arr++ not allowed but arr+1 is allowed ?

The result of a type conversion, is an rvalue.
 
K

Kenneth Brody

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

Correct. (Note, however, that despite its looks, "sizeof(arr)" is
not a function call.)
When we say arr+1, ---> arr is treated as a pointer to char.

When you use the name of an array by itself, it's basically the same
as a pointer to the array.

If you think of "arr" as the same as "&arr[0]", then you can see why
"arr+1" is the same as "&arr[1]".
Why is this anomalous behaviour ?

Also, why arr is called a non modifiable lvalue ?

Because, although it can act like a pointer (for example, you can
pass "arr" to a function expecting "char*"), it is in fact not a
pointer.
why arr++ not allowed but arr+1 is allowed ?

Think of it this way:

int i, *i_ptr;

i_ptr = (&i)++; /* Not allowed */
i_ptr = (&i)+1; /* Allowed */

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
J

john_bode

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

In *most* expression contexts, the type of an array identifier is
converted from "N-element array of T" to "pointer to T", and its value
is converted to the address of the first element of T. Remember that
the array subscripting operation a is *defined* as the expression
*(a+i); for that expression to work, one of a or i needs to be a
pointer value, and the other needs to be an integral value (note that
because of this, a and i[a] are equivalent).

This is also why when you pass an array identifier as an argument to a
function, that parameter is treated as a pointer type instead of an
array type in the called function.

sizeof is one of very few exceptions to this rule, if not the only
exception.
Also, why arr is called a non modifiable lvalue ?

Because although the identifier refers to a region of storage, you are
not allowed to modify that region. Think about it; if you were allowed
to assign a new value to arr, you could lose your only reference to
that region of memory (assigning to arr is *not* the same thing as
assigning to arr[0]).
why arr++ not allowed but arr+1 is allowed ?

The expression arr+1 does not attempt to modify arr. The expression
arr++ does.
 
C

CBFalconer

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.
Why is this anomalous behaviour ?
Also, why arr is called a non modifiable lvalue ?
why arr++ not allowed but arr+1 is allowed ?

Instead of all the bandying about of legalities, take a look at
what is actually going on in a typical system, and then remember
that the legalities etc. are designed to tie everything together on
all systems suitable for the language.

When arr[10] is declared, the compiler assigns a space somewhere,
sufficient to hold 10 chars. It remembers where that space is, and
arranges not to reassign it (at least until it goes out of scope).
That remembering requires a number, call it P. P is going to be
used to create a pointer to the first item in arr whenever needed.

We can't modify P. It just exists as a number. If we did we would
have forgotten where arr lives. We can modify what P points to,
because that is the first item in arr, and that has a home.

If we write arr++ we are trying to modify P. That is not allowed.
The value of the expression arr++ is P+1, which is allowable, but
we can't get it via arr++. So we can write P+1, to get the
unmodifiable pointer to the 2nd item in arr.

What we can do is to declare a pointer, initialize it to point to
arr, and then use ++ on that.

char *ptr = arr;

ptr++;
 
D

Default User

ShiningRay said:
I remember that 'char[]' equals to 'const char*'..

You remember incorrectly. There are no cases that I can think of where
that's true. When used in a function declaration, char[] and char* are
equivalent, but not const char*. In the case of object declarations:

const char *str = "HOWDY WORLD";

and

char str[] = "HOWDY WORLD";

are very different.


I *think* you mean that the name of the array is a constant pointer to
first element. but that's not what you said. That has been discussed in
other posts.



Brian
 
M

Martijn

char arr[10];
When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

arr is the address of the first element, arr[0]. arr + 1 is address
of second element, arr[1], and so on. What is anomalous about this?

A lot of people forget that sizeof is not a function call, more like an
operator. Its result is calculated at compile time. arr is treaded as an
array of 10 characters (hence the size 10), while arr + 1 is "promoted" to a
pointer, hence the result will be the size of a pointer to a char, which is
4 on my system.

Good luck,
 
B

Barry Schwarz

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

It is not anomalous; it is required. The standard specifies that when
an unsubscripted array name appears in an expression, other than as
the operand of the sizeof and & operators, it is converted to a value
equal to the address of the first element of the array with type
"pointer to element". In this newsgroup, this is referred to as THE
RULE.

In sizeof(arr), it is not converted. In arr+1, it is converted.
Also, why arr is called a non modifiable lvalue ?

Because it cannot be modified. It always evaluates to the first
element of the array. It is not a pointer though sometimes it appears
to be treated like one.
why arr++ not allowed but arr+1 is allowed ?

A side effect of arr++ is an attempt to change the value of arr.
arr+1 does not change the value of arr so there is no problem
evaluating the express.


<<Remove the del for email>>
 
J

junky_fellow

pete said:
Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

The name of an array is always converted to a pointer
to it's first element, except in three cases.

N869
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.
Also, why arr is called a non modifiable lvalue ?

Because any attempt to modify an array type expression
results in a situation where the array type expression
is first converted to an rlvalue.
why arr++ not allowed but arr+1 is allowed ?

The result of a type conversion, is an rvalue.

Thanx a lot ....
 
J

junky_fellow

pete said:
Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

The name of an array is always converted to a pointer
to it's first element, except in three cases.

N869
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.
Also, why arr is called a non modifiable lvalue ?

Because any attempt to modify an array type expression
results in a situation where the array type expression
is first converted to an rlvalue.
why arr++ not allowed but arr+1 is allowed ?

The result of a type conversion, is an rvalue.

As you said that array is converted to a pointer to its first element
except in three cases :
1) when it is an operand of sizeof
2) unary &
3) or is string literal, used to initialize an array.

But, don't you think that unary operator ++ and -- should also
be included in this list.

Because when I say, arr++, we are treating it as an array object
and incrementing or decrementing an array-object doesn't make
any sense.
Perhaps, that might be the reason why arr++ or arr--
is not allowed and we call arr to be non-modifiable lvalue.
Because it makes no sense to modify an array object.

But then unary operator should also be a part of cases where
we do not treat array to a pointer to its first element.
 
P

pete

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

The name of an array is always converted to a pointer
to it's first element, except in three cases.

N869
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.
Also, why arr is called a non modifiable lvalue ?

Because any attempt to modify an array type expression
results in a situation where the array type expression
is first converted to an rlvalue.
why arr++ not allowed but arr+1 is allowed ?

The result of a type conversion, is an rvalue.

As you said that array is converted to a pointer to its first element
except in three cases :
1) when it is an operand of sizeof
2) unary &
3) or is string literal, used to initialize an array.

But, don't you think that unary operator ++ and -- should also
be included in this list.

Because when I say, arr++, we are treating it as an array object

No we are not.

(arr++)

means

((char *)arr = (char *)arr + 1)

The result of a type conversion is an rvalue.

Consider

(int)arr[0] = 1; /* bad */

If sizeof(int) is greater than 1,
then where is the second lowest addressable byte of (int)arr[0]?
Nowhere. That space is being used by arr[1].
 
L

Lawrence Kirby

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

arr is the address of the first element, arr[0]. arr + 1 is address
of second element, arr[1], and so on. What is anomalous about this?

A lot of people forget that sizeof is not a function call, more like an
operator.

It is precisely an operator.

Lawrence
 
L

Lawrence Kirby

On Fri, 17 Jun 2005 22:01:49 -0700, Barry Schwarz wrote:

....
Because it cannot be modified. It always evaluates to the first
element of the array. It is not a pointer though sometimes it appears
to be treated like one.

It is an lvalue because it designates an object, it is non-modifiable
because it can't be used to modify that object.

An array name like arr represents the array as a whole, not the first
element which is why, given char arr[10]; then sizeof arr evaluates to
10. When used in a context other than the operand of & or sizeof it
(being an array lvalue) is CONVERTED to a value which is the address of
the first element of the array, i.e. it is a pointer value. So arr
evaluates to &a[0] in the same way that given int i; &i evaluates to the
address of i. In both cases these are just values, no pointer object
exists i.e. nowhere where a pointer value can be STORED.

Lawrence
 
K

Keith Thompson

As you said that array is converted to a pointer to its first element
except in three cases :
1) when it is an operand of sizeof
2) unary &
3) or is string literal, used to initialize an array.

But, don't you think that unary operator ++ and -- should also
be included in this list.

No. What would be the advantage?

x++, in general, adds 1 to the value of x and stores the result in x
(and returns the original value of x).

arr++ is illegal because arr, after conversion, is a pointer value
(not a pointer object), and a pointer *value* cannot be modified, any
more than you can do 42++ or (x+1)++ . If you want to add 1 to the
pointer value, just use arr+1, which is perfectly legal.

Without the implicit conversion, arr++ would be illegal because you
can't increment an array value or assign to an array object.

As long as it's illegal, there's no point in creating another
exception to the rule.
 
Z

zero

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

Also, why arr is called a non modifiable lvalue ?

why arr++ not allowed but arr+1 is allowed ?
 
Z

zero

Consider an array.

char arr[10];

When we find sizeof(arr) ---> Output is 10, arr is treated as
an object of 10 chars.

When we say arr+1, ---> arr is treated as a pointer to char.

Why is this anomalous behaviour ?

Also, why arr is called a non modifiable lvalue ?

why arr++ not allowed but arr+1 is allowed ?

hi,
when we define an array , as
char arr[10] ;

the compiler allocates 10 contiguous bytes .

we can't use statement like , 'arr++' , because , arr is a constant
pointer to
the allocated memory ,

if we change it with 'arr++' , later , the compiler would not be able
to
find the beginning of the array .

but , if we use statement like ' arr+1 ' , we are not modifying the
'arr' , we are just adding 1 to the beginning address the array ,
so , it is ok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top