On Wed, 17 Sep 2003 12:19:10 GMT, "Newsgroup Posting ID"
<newsgrouppostingid@invalid.bigpond.com> wrote:
[color=blue]
> i'm new to c and have gotten my program to run but by passing hardcoded
> array sizes through the routines, i want to make the array extendable by
> using realloc but i'd be doing it two routines down, i need to figure out
> how to pass the array such that i know how many elements are in it before i
> realloc and that the array gets passed back up the routines in tact. i've
> used sizeof but it only works in the first routine, the 2nd and 3rd routines
> result in a size of 4 (presumably the size of the pointer to the array) and
> how would i free the realloc'd memory?
>[/color]
You can't legally realloc() a pointer which was formed, as yours is,
from an actual (declared) array object, only a pointer to allocated
space, that is one which was obtained from malloc or calloc, or a
previous realloc, and not free'd or invalidated by a previous
(successful) realloc.
A function parameter declared as an array of T actually is (rewritten
as) a pointer to T; even if you specify the bound, as you did, it is
ignored. This applies only immediately to the parameter; if you
declare a 2D array (array of array), it is really pointer-to-array.
Since an array used as an argument in the call, or indeed almost
anywhere else, decays to a pointer, this matches the parameter.
As you use in some cases in your snipped code but not others.
See any decent textbook or tutorial or sections 6 and 7 of the FAQ, at
the usual places and
http://www.eskimo.com/~scs/C-faq/top.html .
[color=blue]
> //this is where the array values are currently set and it would be where it
> would be extended via realloc
>[/color]
// comments are not standard in C90, which almost all implementations
still implement, although an easy and fairly common extension; and a
bad idea in Usenet postings anyway as you can see above.
- David.Thompson1 at worldnet.att.net