Mail Archives: djgpp/2000/08/11/09:15:25
Vermin <ratspl AT hotmail DOT com> wrote:
> but is it possible to have an array (or a pointer to an array) which doesn't
> have a a given size (or doesn't really exist) until you define it by making
> space for it in the mem...
That's exactly what pointers and 'new' (or malloc(), if you're writing
C instead of C++) are for.
If you want to do that in a subroutine, it needs that pointer passed
in by reference, so the pointer itself can be modified. I'm not
footsure with the C++ 'reference argument' thingy, so I'll explain how
it'd be done in C. There, pass-by-reference is implemented via another
level of pointer:
> some pseudo code:
> procedure proc1(pointer pp){
> pp (points to) new array[arraySize];
> }
This would be
void proc1(type **pp) {
size_t arraySize;
/* calculate arraySize, somehow */
*pp = malloc(arraySize * sizeof(*pp))
// C++-style would look like this:
// *pp = new type[arraySize];
}
> main(){
> pointer p1;
> call proc1(p1);
> }
and this:
int main(void) {
type *p1 = NULL;
proc1(&p1);
}
An alternative approach would be to have proc1() return a 'type *' as
its result. But that only works for *one* result per function, as
usual.
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -