Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Initializing Pointer to an array

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

[
> > > Unless you are using a C99 compiler, you must return a value from
> > > main().

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


It's not safe to assume that, but it is something to consider.

> I would think that the chances of UB on a hosted
> implentation would be rather high.


The standard doesn't define what the host does with an undefined
status.
But it doesn't define what the host does with a successful status
either.

Hence, if one is 'UB', so is the other!

Pragmatically though, programmers who leave out the return value from
main under C89 are risking future problems in cases where their program
is used in higher level script (e.g. shell script). Such scripting
languages may terminate on spurious error signals from the program.

So 'UB' in this case is probably better phrased as undesirable
behaviour,
rather than undefined.

--
Peter

 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      10-26-2005
Barry Schwarz <(E-Mail Removed)> writes:

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


I gather this has been said already, but briefly:

The equivalence holds, but a requirement for the + operator
is that a pointer operand be a pointer to an object type
(ie, rather than a pointer to an incomplete type). The
variable 'b' is a pointer to an incomplete type, and so
there is a constraint violation.
 
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