pointers to arrays

Ray Butterworth rbutterworth at watmath.waterloo.edu
Sat Feb 18 00:42:53 AEST 1989


In article <19784 at uflorida.cis.ufl.EDU>, thoth at beach.cis.ufl.edu (Robert Forsman) writes:
> 
>   OK, Gordon cross wants to take the address of an array, but not in
> any normal way.  If E is an array (yes I know it's upper case but
> that's what he used) the &E should return a pointer to an array.  My
> question is "what will you use this for?"  Pointers are usually used
> to modify things, and &E I believe would be used to modify the address
> of the array since that's what E is (the address of the array).  YOU
> CAN'T DO THAT!!!

Pointers are also used for passing things to functions.
Consider:

typdef char WorkBuffer[400];

    void
func(buf, argc, argv)
    WorkBuffer *buf;
    char **argv;
{
    /* This function does its thing assuming that buf points
     * to a work buffer containing 400 bytes.
     */
}

    int
main(argc, argv)
    char **argv;
{
    WorkBuffer buf1;
    char buf2[300];

    func(&buf1, argc, argv);
    func(&buf2, argc, argv);

    return 0;
}

Now lint, if it knows what it is doing, would complain about the
second call to func() since the first argument is of the wrong type,
(char (*)[300]) vs. (char (*)[400]).
This is a good thing.

Using the more tradition use of buffers, one would pass it to
a function simply as function(buf, argc, argv), and the parameter
type in the function would be (char *).  If the function is
something like strlen(), that is correct, but if the function
is something that always expects the same length buffer, that
is a bad thing.

Legitimizing pointers to arrays of known size is one of the
good things that the pANS has done.



More information about the Comp.lang.c mailing list