Initializing Pointer to an array

P

Peter Nilsson

Christopher said:
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.
 
T

Tim Rentsch

Barry Schwarz said:
Jack Klein said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top