[comp.lang.c] Doctor C's Pointers

root@wotk.UUCP (Superuser) (08/02/90)

I was reading the August issue of The C Users Journal and came across
this in Rex Jaeschke's article -

(  The ability to subscript a pointer gives rise to the identity )
(                                                                )
(            a[i] is equivalent to *(a + i)                      )
(                                                                )
(  It also allows expressions such as                            )
(                                                                )
(            "abcd"[i] and f()[j]                                )

                           ^^^^^

I thought this was taboo. What's the story on this?

Nick Hennenfent				Voice  404 475-2725
Computone Products			FAX    404 343-9735
1100 Northmeadow Parkway		Usenet ...!uunet!wotk!nickh
Suite 150
Roswell, GA 30076

inst182@tuvie (Inst.f.Techn.Informatik) (08/02/90)

root@wotk.UUCP (Superuser) writes:

>I was reading the August issue of The C Users Journal and came across
>this in Rex Jaeschke's article -

>(  It also allows expressions such as                            )
>(                                                                )
>(            "abcd"[i] and f()[j]                                )

>                           ^^^^^

>I thought this was taboo. What's the story on this?

Why should this be taboo?
Provided f is declared as
	sometype * f (void)
it returns a pointer to sometype, and I can subscript that pointer like
any other pointer. After all, in C arrays are treated like pointers to their
first element in almost any context. (Hope, the wording is correct, I don't
want to go through that Arrays-are-not-pointers discussion again)

Peter J. Holzer


|    _	| Peter J. Holzer			| Think of it	|
| |_|_)	| Technische Universitaet Wien		| as evolution	|
| | |	| hp@vmars.tuwien.ac.at			| in action!	|
| __/  	| ...!uunet!mcsun!tuvie!vmars!hp	|     Tony Rand	|

steve@taumet.com (Stephen Clamage) (08/02/90)

nickh@wotk.UUCP (Nick Hennenfent) writes:

>(            "abcd"[i] and f()[j]                                )
>                           ^^^^^
>I thought this was taboo. What's the story on this?

In ANSI C, it's perfectly ok, as long as f() is declared to return a
pointer type.  Many earlier C compilers did not support this, however,
and its portability was not guaranteed.  Any conforming ANSI compiler
must allow it.

Similarly, if f() returns a struct type with member x, you could write
f().x, and if f() returns a pointer to a function, you could write f()().

I make no comment on whether any of this is good programming practice.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

dbk@mimsy.umd.edu (Dan Kozak) (08/04/90)

From article <1741@tuvie>, by inst182@tuvie (Inst.f.Techn.Informatik):
>>(            "abcd"[i] and f()[j]                                )

> Provided f is declared as
> 	sometype * f (void)
> it returns a pointer to sometype, and I can subscript that pointer like
> any other pointer. 

Or if the declarations are

	int f (void);
	sometype j[SOMECONSTANT];

-- 
#dan

Clever:         dbk@mimsy.umd.edu | "For I was rolled in water,
Not-so-clever:  uunet!mimsy!dbk   |  I was rolled out past the pier" - MoB