Array of Integers ?

A

arnuld

I wrote my blog on Insertion Sort some time back and used words "Array of
Integers" on that. I was told by someone that there is nothing like array
of integers in C. C has array of objects only.

So how do we differentiate between array of integers and array of
characters (not string constant) in communication if there is no such
thing as array of integers ?
 
I

Ike Naar

I wrote my blog on Insertion Sort some time back and used words "Array of
Integers" on that. I was told by someone that there is nothing like array
of integers in C. C has array of objects only.

So how do we differentiate between array of integers and array of
characters (not string constant) in communication if there is no such
thing as array of integers ?

By their names.
 
J

jacob navia

Le 08/11/11 08:51, arnuld a écrit :
I wrote my blog on Insertion Sort some time back and used words "Array of
Integers" on that. I was told by someone that there is nothing like array
of integers in C. C has array of objects only.

So how do we differentiate between array of integers and array of
characters (not string constant) in communication if there is no such
thing as array of integers ?

You define an array of integers like this:

int array[256];

The person that told you that arrays of integers do not exist in C is
completely wrong.

In general you declara an array like this:

TYPE ArrayName [ array-size ] ;

For instance:

double array[256];

or

char array[256];

In general since a standard string is an array of characters finished
by a zero character there is no way of distinguishing it from an array
of characters that is zero terminated.

char array[] = { 'a', 'b', 0};

This has the same representation as the string "ab".
 
N

Nick Keighley

Le 08/11/11 08:51, arnuld a écrit :
I wrote my blog on Insertion Sort some time back and used words "Array of
Integers" on that. I was told by someone that there is nothing like array
of integers in C. C has array of objects only.
So how do we differentiate between array of integers and array of
characters (not string constant) in communication if there is no such
thing as array of integers ?

You define an array of integers like this:

int array[256];

The person that told you that arrays of integers do not exist in C is
completely wrong.

I agree. The array-of-T terminology has been around for an age. cdecl
and such like use the terminology.

Ah, from the standrard (draft '89)

* An array type describes a contiguously allocated set of objects
with a particular member object type, called the element
type .Array
types are characterized by their element type and by the number of
members of the array. An array type is said to be derived from its
element type, and if its element type is T , the array type is
sometimes called ``array of T .'' The construction of an array type
from an element type is called ``array type derivation.''
In general you declara an array like this:

TYPE ArrayName [ array-size ] ;

For instance:

double array[256];

or

char array[256];

In general since a standard string is an array of characters finished
by a zero character there is no way of distinguishing it from an array
of characters that is zero terminated.

char array[] = { 'a', 'b', 0};

This has the same representation as the string "ab".
 
M

Malcolm McLean

I wrote my blog on Insertion Sort some time back and used words "Array of
Integers" on that. I was told by someone that there is nothing like array
of integers in C. C has array of objects only.
C has several integer types, "int" is meant to be the default signed
type and is the "natural integer type" for the machine, which usually
means that it's the same width as a register.

An "object" in C is either an atomic type or a structure or, rarely,
an array. Objects are contiguous in memory. So an array of integers,
whether they be ints, longs, shorts or whatever, is an array of
objects. It's often useful to treat an array as an array of blind
objects, by passing in a void * to the start of the array and the
object width and count. memcpy(), memmove(), qsort() just treat arrays
as lists of bytes, and can be used on ints, doubles, structures, or
any object type.
 
J

James Kuyper

I wrote my blog on Insertion Sort some time back and used words "Array of
Integers" on that. I was told by someone that there is nothing like array
of integers in C. C has array of objects only.

So how do we differentiate between array of integers and array of
characters (not string constant) in communication if there is no such
thing as array of integers ?

Ignore this person. "Array of integers" is nothing more or less than
shorthand for "array of objects of integer type"; the distinction he's
trying to make is pointless.
 
T

tom st denis

In general since a standard string is an array of characters finished
by a zero character there is no way of distinguishing it from an array
of characters that is zero terminated.

char array[] = { 'a', 'b', 0};

This has the same representation as the string "ab".

Exception being you can then do

array[1] = 'c';

Whereas "ab"[1] = 'c' may work but isn't generally a good idea.

Tom
 
N

Nick Keighley

Whereas "ab"[1] = 'c' may work but isn't generally a good idea.

Why not?  Looks like standard C to these old eyes.

it is undefined behaviour to modify a string literal. This allows
compilers to share string representaions or place them in read-only
memory.
 
C

Chad

Le 08/11/11 08:51, arnuld a écrit :
I wrote my blog on Insertion Sort some time back and used words "Array of
Integers" on that. I was told by someone that there is nothing like array
of integers in C. C has array of objects only.
So how do we differentiate between array of integers and array of
characters (not string constant) in communication if there is no such
thing as array of integers ?

You define an array of integers like this:

int array[256];

The person that told you that arrays of integers do not exist in C is
completely wrong.

What about the following...

http://67.40.109.61/torek/c/expr.html

And I quote..

"Array objects have a special, fundamental rule in C. This rule is
essentially arbitrary, and simply must be memorized. It falls out
from a key fact: C does not have array values. (There is one
exception to this, which I will save for later.) C does have array
objects -- just the values are missing. For instance, int a[5];
declares an ordinary array containing five ints. Logically, the
‘value’ of this array ought to be the five int values stored in that
array -- but it is not. Instead, the ‘value’ of the array is a
pointer to the first element of that array. "

The point being that, if I understand correctly, C does not have
'array of integers'. Instead, at least according to the former
comp.lang.c regular on here, the language has 'array objects'.

Chad
 
J

James Kuyper


Note that at the top of that page, the author indicates use of
non-standard language. He claims to be using 'object' to refer to what
the standard calls an 'lvalue'. However, his actual use of the term
doesn't support that claim; he frequently uses that term in ways where
'lvalue' wouldn't fit; in those cases he's actually using it in pretty
much the same way the standard defines it.
"Array objects have a special, fundamental rule in C. This rule is
essentially arbitrary, and simply must be memorized. It falls out
from a key fact: C does not have array values. (There is one
exception to this, which I will save for later.) C does have array
objects -- just the values are missing. For instance, int a[5];
declares an ordinary array containing five ints. Logically, the
‘value’ of this array ought to be the five int values stored in that
array -- but it is not. Instead, the ‘value’ of the array is a
pointer to the first element of that array. "

That paragraph fails to correctly explain what's going on in expressions
like &a or sizeof a, but for most other expressions, it's close enough
to be useful. He gives something closer to the correct rule farther down.

The correct rule is in 6.3.2.1: "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."
The point being that, if I understand correctly, C does not have
'array of integers'. Instead, at least according to the former
comp.lang.c regular on here, the language has 'array objects'.

The discussion has been about an "array of objects", not an "array
object". An array object has sub-objects called elements. Those elements
have a type, and an array of objects which have the type T are routinely
referred to as an "array of T"; this usage is common in the standard
itself. The first five examples I found of such usage were 6.2.5p20,
footnote 36, 6.2.5p29, 6.3.2.1p3, and 6.5p6; there's many more. Nothing
said on the page you're referring to is incompatible with that usage.
 
B

Ben Bacarisse

Chad said:
Le 08/11/11 08:51, arnuld a écrit :
I wrote my blog on Insertion Sort some time back and used words "Array of
Integers" on that. I was told by someone that there is nothing like array
of integers in C. C has array of objects only.
So how do we differentiate between array of integers and array of
characters (not string constant) in communication if there is no such
thing as array of integers ?

You define an array of integers like this:

int array[256];

The person that told you that arrays of integers do not exist in C is
completely wrong.

What about the following...

http://67.40.109.61/torek/c/expr.html

And I quote..

"Array objects have a special, fundamental rule in C. This rule is
essentially arbitrary, and simply must be memorized. It falls out
from a key fact: C does not have array values. (There is one
exception to this, which I will save for later.) C does have array
objects -- just the values are missing. For instance, int a[5];
declares an ordinary array containing five ints. Logically, the
‘value’ of this array ought to be the five int values stored in that
array -- but it is not. Instead, the ‘value’ of the array is a
pointer to the first element of that array. "

The point being that, if I understand correctly, C does not have
'array of integers'. Instead, at least according to the former
comp.lang.c regular on here, the language has 'array objects'.

No, that quote says that C has no array values, not that it has no
arrays. The point is not that an array of ints does not exist, but that
there is no way to denote it's value.

This is unlike the behaviour of many other types in C. Given

int i = 42;
struct point { int x, y; } p = { 1, 2 };
int ia[5] = { 1, 2, 3, 4, 5 };

the expression 'i' denotes the value of i, and the expression 'p'
denotes the value of p. You can't do this with an array. The
expression 'ia' does not denote the array named ia -- it is an
expression of type pointer to int.

It's a moot point whether the sub-expression 'ia' denotes the array in
the expression 'sizeof ia'. I'd go with "no" since the sub-expression
is not evaluated.
 
K

Keith Thompson

Noob said:
Bradley said:
Tom said:
Whereas "ab"[1] = 'c' may work but isn't generally a good idea.

Why not? Looks like standard C to these old eyes.

String literals may not be modified.

Correction: The behavior of a program that attempts to modify a
string literal is undefined. The conclusion is the same: Don't
Do That. The difference is that the compiler isn't obligated to
diagnose the attempt (and it *could* "work").
 
J

James Kuyper

On 11/08/2011 04:45 PM, Ben Bacarisse wrote:
....
This is unlike the behaviour of many other types in C. Given

int i = 42;
struct point { int x, y; } p = { 1, 2 };
int ia[5] = { 1, 2, 3, 4, 5 };

the expression 'i' denotes the value of i, and the expression 'p'
denotes the value of p. You can't do this with an array. The
expression 'ia' does not denote the array named ia -- it is an
expression of type pointer to int.

It's a moot point whether the sub-expression 'ia' denotes the array in
the expression 'sizeof ia'. I'd go with "no" since the sub-expression
is not evaluated.

How about in &ia?
 
J

jacob navia

Le 08/11/11 15:23, tom st denis a écrit :
In general since a standard string is an array of characters finished
by a zero character there is no way of distinguishing it from an array
of characters that is zero terminated.

char array[] = { 'a', 'b', 0};

This has the same representation as the string "ab".

Exception being you can then do

array[1] = 'c';

Whereas "ab"[1] = 'c' may work but isn't generally a good idea.

Tom

I said:

The same representation. Nothing else. Some compilers
put immediate strings constants in a read only section,
some not. The standard says those shouldn't be modified.

OK.

But what I said still holds:
char array[] = { 'a', 'b', 0};

This has the same representation as the string "ab".
 
K

Keith Thompson

Chad said:
What about the following...

http://67.40.109.61/torek/c/expr.html

And I quote..

"Array objects have a special, fundamental rule in C. This rule is
essentially arbitrary, and simply must be memorized. It falls out
from a key fact: C does not have array values. (There is one
exception to this, which I will save for later.) C does have array
objects -- just the values are missing. For instance, int a[5];
declares an ordinary array containing five ints. Logically, the
‘value’ of this array ought to be the five int values stored in that
array -- but it is not. Instead, the ‘value’ of the array is a
pointer to the first element of that array. "

The point being that, if I understand correctly, C does not have
'array of integers'. Instead, at least according to the former
comp.lang.c regular on here, the language has 'array objects'.

I find the quoted text misleading at best.

C does have array values; it just doesn't let you manipulate them
directly in most cases. The standard's definition of the word
"value" (C99 3.17) is "precise meaning of the contents of an object
when interpreted as having a speciï¬c type". Nothing in that
definition excludes arrays. Given:

int arr[5];

the value of arr consists of the values of its 5 elements,
interpreted as having type "int[5]". When you store something
into an array object, that object has a value, and that value is
an array value.

The relevant rule (stated in C99 6.3.2.1p3) is that an expression
of array type is implicitly converted to a (non-lvalue) pointer to
the array object's first element -- *unless* the array expression
is the operand of a unary "&" (address-of) or of "sizeof", or is
a string literal in an initializer used to initialize an array;
in those cases, an array expression really is an array expression.

Note that when we refer to "an array", we generally mean "an object
of array type", or equivalently "an array object".

arr, as defined above is an array object (of type int[5]). Like any
array object, it is an array of objects (those objects happen to
be of type int). More precisely, it's an array of integers (an
array of longs, or of chars, would also be an array of integers).
Even more precisely, it's an array of ints.
 
K

Keith Thompson

Malcolm McLean said:
C has several integer types, "int" is meant to be the default signed
type and is the "natural integer type" for the machine, which usually
means that it's the same width as a register.

An "object" in C is either an atomic type or a structure or, rarely,
an array.

What's rare about array objects?

An "object" is, by definition, a "region of data storage in the
execution environment, the contents of which can represent values"
(C99 3.14). It can be of any type other than an incomplete or
function type: scalar (including arithmetic and pointer ("atomic"
means something else)), struct, union, or array.
Objects are contiguous in memory. So an array of integers,
whether they be ints, longs, shorts or whatever, is an array of
objects.

Yes. An array of integers is also an object itself. Given:

int arr[5];

arr is an object of type int[5], and each of its 5 elements is an
object of type int.

[...]
 
K

Keith Thompson

James Kuyper said:
On 11/08/2011 04:45 PM, Ben Bacarisse wrote:
...
This is unlike the behaviour of many other types in C. Given

int i = 42;
struct point { int x, y; } p = { 1, 2 };
int ia[5] = { 1, 2, 3, 4, 5 };

the expression 'i' denotes the value of i, and the expression 'p'
denotes the value of p. You can't do this with an array. The
expression 'ia' does not denote the array named ia -- it is an
expression of type pointer to int.

It's a moot point whether the sub-expression 'ia' denotes the array in
the expression 'sizeof ia'. I'd go with "no" since the sub-expression
is not evaluated.

How about in &ia?

It refers to the array object. It doesn't refer to the value of that
object. (I maintain that it does have a value (see my other followup),
but it's not used here.)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top