Array of Integers ?

Discussion in 'C Programming' started by arnuld, Nov 8, 2011.

  1. arnuld

    arnuld Guest

    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 ?



    --
    arnuld
    http://LispMachine.Wordpress.com
     
    arnuld, Nov 8, 2011
    #1
    1. Advertising

  2. arnuld

    Ike Naar Guest

    On 2011-11-08, arnuld <> wrote:
    > 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.
     
    Ike Naar, Nov 8, 2011
    #2
    1. Advertising

  3. arnuld

    jacob navia Guest

    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".
     
    jacob navia, Nov 8, 2011
    #3
  4. On Nov 8, 8:14 am, jacob navia <> wrote:
    > 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".
     
    Nick Keighley, Nov 8, 2011
    #4
  5. On Nov 8, 9:51 am, arnuld <> wrote:
    >
    > 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.

    --
    Basic Algorithms: advance your knowledge of C programming
    http://www.malcolmmclean.site11.com/www
     
    Malcolm McLean, Nov 8, 2011
    #5
  6. arnuld

    James Kuyper Guest

    On 11/08/2011 02:51 AM, arnuld wrote:
    > 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.
    --
    James Kuyper
     
    James Kuyper, Nov 8, 2011
    #6
  7. arnuld

    tom st denis Guest

    On Nov 8, 3:14 am, jacob navia <> wrote:
    > 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
     
    tom st denis, Nov 8, 2011
    #7
  8. In article <>,
    tom st denis <> wrote:
    > ...
    >Whereas "ab"[1] = 'c' may work but isn't generally a good idea.


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

    --bks
     
    Bradley K. Sherman, Nov 8, 2011
    #8
  9. On Nov 8, 2:28 pm, (Bradley K. Sherman) wrote:
    > In article <..com>,
    > tom st denis  <> wrote:



    > >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.
     
    Nick Keighley, Nov 8, 2011
    #9
  10. arnuld

    Chad Guest

    On Nov 8, 12:14 am, jacob navia <> wrote:
    > 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
     
    Chad, Nov 8, 2011
    #10
  11. arnuld

    Noob Guest

    Bradley K. Sherman wrote:

    > Tom wrote:
    >
    >> 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.
     
    Noob, Nov 8, 2011
    #11
  12. In article <j9bhk9$jh8$>, Noob <root@127.0.0.1> wrote:
    >Bradley K. Sherman wrote:
    >
    >> Tom wrote:
    >>
    >>> 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.


    Quite right, I retract my question.

    --bks
     
    Bradley K. Sherman, Nov 8, 2011
    #12
  13. arnuld

    James Kuyper Guest

    On 11/08/2011 10:12 AM, Chad wrote:
    > On Nov 8, 12:14 am, jacob navia <> wrote:

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


    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.
     
    James Kuyper, Nov 8, 2011
    #13
  14. Chad <> writes:

    > On Nov 8, 12:14 am, jacob navia <> wrote:
    >> 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.

    --
    Ben.
     
    Ben Bacarisse, Nov 8, 2011
    #14
  15. Noob <root@127.0.0.1> writes:
    > Bradley K. Sherman wrote:
    >> Tom wrote:
    >>> 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").

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 8, 2011
    #15
  16. arnuld

    James Kuyper Guest

    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?
     
    James Kuyper, Nov 8, 2011
    #16
  17. arnuld

    jacob navia Guest

    Le 08/11/11 15:23, tom st denis a écrit :
    > On Nov 8, 3:14 am, jacob navia<> wrote:
    >> 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:

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


    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".
     
    jacob navia, Nov 8, 2011
    #17
  18. Chad <> writes:
    [...]
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 8, 2011
    #18
  19. Malcolm McLean <> writes:
    > On Nov 8, 9:51 am, arnuld <> wrote:
    >> 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.


    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.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 8, 2011
    #19
  20. James Kuyper <> writes:
    > 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.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 8, 2011
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    8,111
    Ralf Hildebrandt
    Jan 19, 2005
  2. Replies:
    12
    Views:
    14,609
    Lee Ryman
    Apr 26, 2005
  3. Replies:
    8
    Views:
    836
    Oliver Wong
    Jan 27, 2006
  4. Royan
    Replies:
    32
    Views:
    1,025
  5. Rob Meade

    Array Integers vs Array Variables

    Rob Meade, Feb 8, 2004, in forum: ASP General
    Replies:
    16
    Views:
    259
    Roland Hall
    Feb 8, 2004
Loading...

Share This Page