Re: pointer to an int array

From: S.Tobias (siXtY_at_FamOuS.BedBuG.pAlS.INVALID)
Date: 01/07/05


Date: 7 Jan 2005 22:31:16 GMT

Mark McIntyre <markmcintyre@spamcop.net> wrote:
> On Mon, 3 Jan 2005 14:01:44 +0530, in comp.lang.c , "Neo"
> <timeless_illusion@yahoo.com> wrote:

> >if I change the declaration above as :
> >
> > int (*p)[]; <-- pointer2array.c:12: error: invalid use of array with unspecified bounds
> >
> >compiler error!

> yes, its an error

IMHO I think you're wrong here. `p' is simply a pointer to incomplete type
("array of int of unknown size").

The OP probably hadn't noticed that the error was at different line:
  printf("%d\n", p[0][i]); //error
Exactly, the offending expression is p[0], because, it gets translated
to:
  *(p + 0)
in which the summation cannot be evaluated because `*p' has an incomplete
type (however I fail to explain exactly why, because this expression doesn't
seem to violate any constraints at the "+" operator; could anyone help?).

The above expression can be corrected with:
  (*p)[i];
This is interesting, because we seem to apply indirection operator to
pointer to incomplete type (again, this is not a constraint violation,
however it doesn't work with pointers to incomplete struct type; could
someone help explain this please?); but here lvalue `*p', which is
of "array of int" type, is converted to "pointer to int" and points
to its first element, essentially same location as the array itself;
this rule seems to take precedence.

> >what's the use of array index in the above declaration?

Type `int[]' is compatible with `int[10]' and `int[20]',
so `p' could be assigned an address of objects defined as:
  int a[15];
  int b[80];
  double d[10]; //except this object
  p = &a;
  p = &b;
  p = &d; //error
However, since `p' is a pointer to incomplete type, no arithmetic or
comparisons can be made with it.
I think one can describe `p' as a generic pointer to "arrays of int".
I don't know if it could be more useful than that.

> >as i can go upto 12 or more...

> only by invoking undefined behaviour....

Yes.

-- 
Stan Tobias
mailx `echo siXtY@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`


Relevant Pages

  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: Newbie
    ... to talk about the int value 3 and the int value 4, ... It also lets you talk about pointer ... C has a special rule for array objects. ... to printf() is: ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)
  • Re: How to pass a pointer to an unknown-size array?
    ... > I can pass a "pointer to a double" to a function that accepts ... > int func{ ... > Now I want to pass a pointer to an array of doubles, ...
    (comp.lang.c)