Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Initializing Pointer to an array

Reply
Thread Tools

Initializing Pointer to an array

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-21-2005
Peter Nilsson <(E-Mail Removed)> wrote:

> It's not mandatory un C90, just desirable.


I suppose that's strictly correct, but it's probably safe to assume
that a reasonable host environment will attempt to use the termination
status of main. I would think that the chances of UB on a hosted
implentation would be rather high.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Zara
Guest
Posts: n/a
 
      10-21-2005
On Fri, 21 Oct 2005 14:34:06 +0000 (UTC), Christopher Benson-Manica
<(E-Mail Removed)> wrote:

>Peter Nilsson <(E-Mail Removed)> wrote:
>
>> It's not mandatory un C90, just desirable.

>
>I suppose that's strictly correct, but it's probably safe to assume
>that a reasonable host environment will attempt to use the termination
>status of main. I would think that the chances of UB on a hosted
>implentation would be rather high.


C *and* C++ specify that reaching the the terminating } of main() will
return 0; so this function *always* return a valid value.
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      10-21-2005
Zara <(E-Mail Removed)> wrote:

> C *and* C++ specify that reaching the the terminating } of main() will
> return 0; so this function *always* return a valid value.


C89 does not.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-21-2005
Christopher Benson-Manica wrote:
> Zara <(E-Mail Removed)> wrote:
>
>>C *and* C++ specify that reaching the the terminating } of main() will
>>return 0; so this function *always* return a valid value.

>
> C89 does not.


And, just to emphasise the point, there are far more people using
implementations that can support C89 correctly (modulo bugs) than there
are people using conforming C99 implementations. For a start, neither
gcc nor MS VC++ fully support C99 but both fully support C89 (modulo bugs).
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Tim Rentsch
Guest
Posts: n/a
 
      10-21-2005
Jack Klein <(E-Mail Removed)> writes:

> Plus the C language absolutely requires that the expression b[0] be
> evaluated as if written *b,


Usually but not always.

void
foo(){
int a[10];
int (*b)[] = &a;
int *c;

c = b[0]; /* this doesn't work */
c = *b; /* this works */
}

Personally, I'd be in favor of adding a rule that makes the two
forms equivalent in the case of a 0-valued constant expression.
But that's another story.

 
Reply With Quote
 
Antonio Contreras
Guest
Posts: n/a
 
      10-21-2005
Jack Klein wrote:
> On 20 Oct 2005 15:36:14 -0700, "Antonio Contreras" <(E-Mail Removed)>
> wrote in comp.lang.c:
>
> > Flash Gordon wrote:
> > > Christopher Benson-Manica wrote:
> > > > Antonio Contreras <(E-Mail Removed)> wrote:
> > > >
> > > >>for (i = 0; i < ((sizeof b) / (sizeof int)); i++)
> > > >
> > > > Yes. Ouch. (And thank you.)
> > >
> > > Or better yet:
> > > for (i = 0; i < ((sizeof b) / (sizeof *b)); i++)
> > >
> > > Then it will compile (apart from being incomplete and the certainty of
> > > me introducing an error in my correction) and be independant of the type
> > > of array b.
> > >
> > >

> >
> > Well, for purely aesthetic reasons I would prefer the totally
> > equivalent:
> >
> > for (i = 0; i < ((sizeof b) / (sizeof b[0])); i++)
> >
> > Call me paranoid, but the equivalence between arrays and pointers is
> > confusing enough [1] without dereferencing pointers that have been
> > created by the decay of an array name.

>
> The sizeof operator never evaluates the value of its operand in C
> prior to C99, and only to determine the size of a variable length
> array in C99. Under no circumstances does sizeof dereference a
> pointer given to it as an argument.


I didn't imply that. What I tried to say is that it "looks" like it,
and that it is confusing when you're learning C. IMHO, given that b is
an array, b[0] is less confusing than *b.

> Plus the C language absolutely requires that the expression b[0] be
> evaluated as if written *b, so the Christopher's code and your
> alternative are exactly equivalent in C.


I acknowledge that much. I literally said "I prefer the _totally
equivalent_..."

> > [1] Actually I've gotten used to it and it's not confusing anymore, but
> > it was really confusing two years ago.

>
> In either case, there is no evaluation of the value of b[0] or *b, and
> no dereference.


Again, I didn't try to imply that. I guess it was a really bad wording
from my side.

 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-22-2005
On 21 Oct 2005 12:22:00 -0700, Tim Rentsch <(E-Mail Removed)>
wrote:

>Jack Klein <(E-Mail Removed)> writes:
>
>> Plus the C language absolutely requires that the expression b[0] be
>> evaluated as if written *b,

>
>Usually but not always.
>
> void
> foo(){
> int a[10];
> int (*b)[] = &a;
> int *c;
>
> c = b[0]; /* this doesn't work */
> c = *b; /* this works */


Are you sure? n1124 states in 6.5.2.1(2) that

A postfix expression followed by an expression in square brackets []
is a subscripted designation of an element of an array object. The
definition of the subscript operator [] is that E1[E2] is identical to
(*((E1)+(E2))). Because of the conversion rules that apply to the
binary + operator, if E1 is an array object (equivalently, a pointer
to the initial element of an array object) and E2 is an integer,
E1[E2] designates the E2-th element of E1 (counting from zero).

So b[0] is the same as *(b+0) which is the same as *b. Both
expressions should evaluate to the address of a with type array of 10
int which, since it is not one of the exceptions, will then be
converted to the address of a[0] with type pointer to int.

> }
>
>Personally, I'd be in favor of adding a rule that makes the two
>forms equivalent in the case of a 0-valued constant expression.
>But that's another story.



<<Remove the del for email>>
 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      10-22-2005
Barry Schwarz <(E-Mail Removed)> wrote:
> On 21 Oct 2005 12:22:00 -0700, Tim Rentsch <(E-Mail Removed)>
> wrote:
>>Jack Klein <(E-Mail Removed)> writes:
>>
>>> Plus the C language absolutely requires that the expression b[0] be
>>> evaluated as if written *b,

>>
>>Usually but not always.
>>
>> void
>> foo(){
>> int a[10];
>> int (*b)[] = &a;
>> int *c;
>>
>> c = b[0]; /* this doesn't work */
>> c = *b; /* this works */

>
> Are you sure? n1124 states in 6.5.2.1(2) that
>
> A postfix expression followed by an expression in square brackets []
> is a subscripted designation of an element of an array object. The
> definition of the subscript operator [] is that E1[E2] is identical to
> (*((E1)+(E2))). Because of the conversion rules that apply to the
> binary + operator, if E1 is an array object (equivalently, a pointer
> to the initial element of an array object) and E2 is an integer,
> E1[E2] designates the E2-th element of E1 (counting from zero).
>
> So b[0] is the same as *(b+0) which is the same as *b.


They are not the same. `b+0' has an additional constraint:
6.5.6#2
# [#2] For addition, either both operands shall have
# arithmetic type, or one operand shall be a pointer to an
# object type and the other shall have integer type.

("Object type" is a complete type.)

>Both
> expressions should evaluate to the address of a with type array of 10
> int which,


`*b' is an incomplete (array) type lvalue.

>since it is not one of the exceptions, will then be
> converted to the address of a[0] with type pointer to int.


Yes.

--
Stan Tobias
mailx `echo http://www.velocityreviews.com/forums/(E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      10-22-2005
S.Tobias wrote:
> Barry Schwarz <(E-Mail Removed)> wrote:
>
>>On 21 Oct 2005 12:22:00 -0700, Tim Rentsch <(E-Mail Removed)>
>>wrote:
>>
>>>Jack Klein <(E-Mail Removed)> writes:
>>>
>>>
>>>>Plus the C language absolutely requires that the expression b[0] be
>>>>evaluated as if written *b,
>>>
>>>Usually but not always.
>>>
>>> void
>>> foo(){
>>> int a[10];
>>> int (*b)[] = &a;
>>> int *c;
>>>
>>> c = b[0]; /* this doesn't work */
>>> c = *b; /* this works */

>>
>>Are you sure? n1124 states in 6.5.2.1(2) that
>>
>>A postfix expression followed by an expression in square brackets []
>>is a subscripted designation of an element of an array object. The
>>definition of the subscript operator [] is that E1[E2] is identical to
>>(*((E1)+(E2))). Because of the conversion rules that apply to the
>>binary + operator, if E1 is an array object (equivalently, a pointer
>>to the initial element of an array object) and E2 is an integer,
>>E1[E2] designates the E2-th element of E1 (counting from zero).
>>
>>So b[0] is the same as *(b+0) which is the same as *b.

>
>
> They are not the same. `b+0' has an additional constraint:
> 6.5.6#2
> # [#2] For addition, either both operands shall have
> # arithmetic type, or one operand shall be a pointer to an
> # object type and the other shall have integer type.
>
> ("Object type" is a complete type.)


Yes.

>>Both
>>expressions should evaluate to the address of a with type array of 10
>>int which,

>
> `*b' is an incomplete (array) type lvalue.


*b being an incomplete type is irrelevant since it is *(b+0) NOT *b +0
so 0 is added to b not to *b.

>>since it is not one of the exceptions, will then be
>>converted to the address of a[0] with type pointer to int.

>
> Yes.

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      10-24-2005
Flash Gordon <(E-Mail Removed)> wrote:
> S.Tobias wrote:
>> Barry Schwarz <(E-Mail Removed)> wrote:
>>>On 21 Oct 2005 12:22:00 -0700, Tim Rentsch <(E-Mail Removed)>
>>>wrote:


>>>> void
>>>> foo(){
>>>> int a[10];
>>>> int (*b)[] = &a;
>>>> int *c;
>>>>
>>>> c = b[0]; /* this doesn't work */
>>>> c = *b; /* this works */


[snip]
>>>So b[0] is the same as *(b+0) which is the same as *b.

....
>>>Both
>>>expressions should evaluate to the address of a with type array of 10
>>>int which,

>>
>> `*b' is an incomplete (array) type lvalue.

>
> *b being an incomplete type is irrelevant since it is *(b+0) NOT *b +0
> so 0 is added to b not to *b.
>

Right. I did mean `*b', but I misunderstood what Barry had said, so my
answer was irrelevant, too.

One more try: `*b' evaluates to an incomplete array type lvalue
for the array `a', which is immediately converted to the pointer to
the first element of `a' (not: "to the address of `a'"; unless "address"
is understood as "location").

--
Stan Tobias
mailx `echo (E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to array of array of const pointer RSL C++ 14 02-19-2010 02:06 PM
Array of pointer and pointer of array erfan C Programming 6 01-28-2008 08:55 PM
initializing array using pointer arun C Programming 31 12-15-2005 09:42 AM
Array of pointer Vs Pointer to Array sangeetha C Programming 9 10-09-2004 07:01 PM



Advertisments