Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!vrdxhq!dgis!bms-at!stuart From: stuart@bms-at.UUCP (Stuart D. Gathman) Newsgroups: comp.lang.c Subject: Re: pointers to arrays Message-ID: <274@bms-at.UUCP> Date: Thu, 20-Nov-86 15:51:55 EST Article-I.D.: bms-at.274 Posted: Thu Nov 20 15:51:55 1986 Date-Received: Fri, 21-Nov-86 05:46:06 EST References: <273@bms-at.UUCP> <1138@genrad.UUCP> Organization: Business Management Systems, Inc., Fairfax, VA Lines: 108 Keywords: What? Why? How? Summary: Useless replies, a restatement of the problem. In article <1138@genrad.UUCP>, rep@genrad.UUCP (Pete Peterson) writes: > In article <273@bms-at.UUCP> stuart@bms-at.UUCP (Stuart D. Gathman) writes: > >1) How does one get such an animal? The only methods I can figure are > > a) a cast: ( type (*)[] ) array_of_type > > b) a function returning such a type (but the return must use a cast!) > [ example of pointer to array declaration deleted ] I am quite aware of how to *declare* pointers to arrays (as a cursory perusal of my posting will reveal). I want to know how to get a pointer to an array in an expression! The key problem is that arrays are always converted to pointers to their first elements. > typedef char line[81]; > typedef line *linept; > linept linesptr; > line page[20]; /* declare an array of 20 "lines" */ > char *lpointer; main() { > lpointer = linesptr = page; /* point at first char of */ } /* the above does not compile or lint (I tried it on 2 machines). A quick analysis will reveal the point of my previous posting (I hope). The reference to 'page' is automatically converted to a pointer to its first element. This is how arrays are treated in 'C'. This (of type linept) is assigned to linesptr without error. 'linesptr' is not the correct type to assign to lpointer. */ > >3) It seems to me that any distinction between a pointer to an array > > and a pointer to its first element is purely semantic. (And given > > the semantic difficult of obtaining a pointer to an array, why use > > them?) There is no pointer conversion that I can imagine involved. > [ example of pointer arithmetic deleted ] Given a pointer to the first element, I can increment it by the number of elements in the array. Having a pointer to an array conveniently remembers the number of elements for me (this would be especially handy with dynamically sized arrays a la algol). But, the aggravation of having to use casts to get such a pointer into an expression defeats the purpose! *************** *** A Practical Example ********** *************** /* Let's suppose we want to use: */ #include /* We want to have a pointer to a jmp_buf for use by error handling code */ jmp_buf *envptr; /* envptr is a pointer to an array ! */ { jmp_buf local_env; /* this is an array of int's */ envptr = &local_env; /* oops! illegal! */ /* The above is very confusing, especially since there is no explanation for the error until you find the typedef for jmp_buf in setjmp.h! */ if (!setjmp(local_env)) { /* . . . */ } } /* how are we supposed to assign a pointer to jmpbuf to envptr ? */ /* I use: */ typedef struct { jmp_buf env; } JMP_BUF; JMP_BUF *env; /* using global pointer lets you nest setjmps ! */ { JMP_BUF local_env; env = &local_env; if (!setjmp(local_env.env)) { /* . . . */ if (error) longjump(env->env); /* . . . */ } /* cleanup */ } ************* IS THERE A BETTER WAY ? ************* P.S. If you're going to suggest: jmp_buf loc_env[1]; Don't. I like the structure better. -- Stuart D. Gathman <..!seismo!{vrdxhq|dgis}!bms-at!stuart>