Re: pointer to an int array
From: S.Tobias (siXtY_at_FamOuS.BedBuG.pAlS.INVALID)
Date: 01/07/05
- Next message: mfhaigh_at_sbcglobal.net: "Re: tricky assignment statemenent"
- Previous message: Christopher Benson-Manica: "Re: /dev/urandom vs. /dev/random"
- In reply to: Mark McIntyre: "Re: pointer to an int array"
- Next in thread: Lawrence Kirby: "Re: pointer to an int array"
- Reply: Lawrence Kirby: "Re: pointer to an int array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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`
- Next message: mfhaigh_at_sbcglobal.net: "Re: tricky assignment statemenent"
- Previous message: Christopher Benson-Manica: "Re: /dev/urandom vs. /dev/random"
- In reply to: Mark McIntyre: "Re: pointer to an int array"
- Next in thread: Lawrence Kirby: "Re: pointer to an int array"
- Reply: Lawrence Kirby: "Re: pointer to an int array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|