POV-Ray : Newsgroups : povray.off-topic : Tell me it isn't so! : Re: Tell me it isn't C Server Time
15 Nov 2024 10:18:44 EST (-0500)
  Re: Tell me it isn't C  
From: Warp
Date: 22 Jul 2009 13:43:51
Message: <4a674fd7@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> True. C doesn't have arrays, really. At least, none with actual names.

  Actually C *does* have arrays. For example, this is an array:

int table[10];

  Here 'table' is *not* a pointer to a memory location containing space
for 10 ints. 'table' is an array of 10 ints. While it behaves a lot like
a pointer (and is implicitly cast to an int* when necessary), there are
significant differences compared to a pointer. For instance, sizeof(table)
is not the same as sizeof(int*). Also, you can't make 'table' "point" to
somewhere else (because it's not a pointer).

  These are stack-allocated arrays. It is also possible to create
heap-allocated arrays, although not directly. You have to enclose it
inside a struct, like this:

struct ArrayContainer
{
    int table[10];
};

  Here 'table' is also *not* a pointer, it's an array. Now you can allocate
instances of that struct with malloc() and you will effectively have a
heap-allocated array.

  (Granted, C arrays are a bit rigid and non-dynamic, but they are still
arrays by any definition.)

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.