473,395 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

array questions

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 ?

Nov 14 '05 #1
18 2378
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.

<ju**********@yahoo.co.in> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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 ?

Nov 14 '05 #2
ju**********@yahoo.co.in wrote:
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).

Nov 14 '05 #3
ju**********@yahoo.co.in wrote:

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.

--
pete
Nov 14 '05 #4


ju**********@yahoo.co.in wrote:
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[i] 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[i] 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.

Nov 14 '05 #5
ju**********@yahoo.co.in wrote:

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:Th*************@gmail.com>
Nov 14 '05 #6
ju**********@yahoo.co.in wrote:

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++;

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #7
I remember that 'char[]' equals to 'const char*'..

Nov 14 '05 #8


ShiningRay wrote:
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

Nov 14 '05 #9
>> 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,

--
Martijn
http://www.sereneconcepts.nl
Nov 14 '05 #10
On 17 Jun 2005 06:21:49 -0700, ju**********@yahoo.co.in wrote:
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>>
Nov 14 '05 #11


pete wrote:
ju**********@yahoo.co.in wrote:

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.

--
pete


Thanx a lot ....

Nov 14 '05 #12


pete wrote:
ju**********@yahoo.co.in wrote:

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.

--
pete


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.

Nov 14 '05 #13
ju**********@yahoo.co.in wrote:

pete wrote:
ju**********@yahoo.co.in wrote:

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.

--
pete


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].

--
pete
Nov 14 '05 #14
On Fri, 17 Jun 2005 23:55:58 +0200, Martijn wrote:
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
Nov 14 '05 #15
On Fri, 17 Jun 2005 22:01:49 -0700, Barry Schwarz wrote:

....
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.


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
Nov 14 '05 #16
ju**********@yahoo.co.in writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #17


ju**********@yahoo.co.in wrote:
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 ?


Nov 14 '05 #18


ju**********@yahoo.co.in wrote:
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

Nov 14 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Joe Six-Pack | last post by:
Hi, Im having problems in randomly selecting an element in an array that has not been selected before.. in other words, I have an array of answers to questions, then I want to select 5, with one...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: Robert | last post by:
Hi, This might be a strange question but i would like to know how to return an array from a function. Do you have to use pointers for this? Thanks in advance, Robert
28
by: anonymous | last post by:
I have couple of questions related to array addresses. As they belong to the same block, I am putting them here in one single post. I hope nobody minds: char array; int address; Questions...
3
by: kurrent | last post by:
i'm still new to php and programming and was hoping to get some help on a few questions i haven't had success in answering myself. I successfully created my first very simple script to accomplish a...
272
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
2
by: Imran | last post by:
Hello all, I am trying to pass 2D array to a func, as follows, it is showing compiler error. void helloptr(int **a){ a = 0xB; } int main(int argc, char* argv){
7
by: lawpoop | last post by:
Hello all - Is there a way to get a nested array in a $_POST variable? I have a form where there are several questions, each one corresponding to a database row. On submission of the form, I...
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.