8/5 In ANSI C, assume a typedef of Foo for an UnsignedInt16 ,
then assume that I declare a multiarray Foo foo[a][b] = {..}.
How do I assign a pointer to foo? Do I do a Foo **bar?
If Foo **bar then bar=foo doesn't seem to work.
\_ References to an array name only decay to pointers for one
level. So do bar = &foo[0].x
\_ huh? what the hell is x?
\_ x is the first element of type Foo
\_ x is someone slipping up with emacs.
\_ This doesn't make any sense. A multidimensional array does
not contain actual pointers to any of its elements, so there
is nothing for bar to point at. If you do foo[0], this
standard notation. Also, foo *bar can be assigned to bar = foo
if foo is Foo[a]
produced only a temporary pointer; getting the address of
if foo is Foo foo[a].
this is not a sensible thing to do.
\_How so? Main has char **argv, and I can reference it using
standard notation. Also, Foo *bar can be assigned to bar = foo
if foo is Foo foo[a]. So why wouldn't there be an analogous
expression for a pointer to a multidimensional array? If
what you say is true, then is it impossible to reference a
multiarray using a pointer? How would I go about passing
multiarrays to functions then which would like to store a
pointer to the multiarray?
\_ your C-fu is lacking. The declaration T foo[a][b] allocates a
contiguous chunk of memory that's a*b*sizeof(T) bytes long.
argv, on the other hand, is an array of pointers and is not
\_ sorry, I misspoke. I meant
to say that it is a pointer
to an array of pointers.
a true multidimensional array. The fact that you can
dereference elements to both using [] operators is syntactic
convenience. Go read the comp.lang.C FAQ, section 6 in
particular:
http://www.eskimo.com/~scs/C-faq/top.html
\_Yes, I know that foo[a][b] allocates a contiguous chunk of
memory, so does foo[a]. What I have a problem with is the fact
that foo[a] can decay into a pointer but not foo[a][b]. I
read the FAQ, and I think essentially the problem is is that
ANSI C provides no method of recursive pointer decay from
a multiarray. I don't know why not, perhaps it has something
to do with compiler performance issues. So I suppose i
the short answer is
"no, it can't be done" and I solved the issue by dynamically
allocating a multiarray using a pointer pointer.
\_ Why should foo[a][b] decay into a pointer?
\_ Hmm, I learned C 12 years ago and I still find this section
of the FAQ very useful.
\_ What you can do is Foo (*bar)[b]; bar = foo;
\_ shouldn't that be: Foo (*bar)(b); bar = foo;
\_ uh, he's not declaring a function pointer.
\_ If the OP has their answer from the above would you please delete
this thread. I'm losing 1d4+1 sanity points everytime my eyes pass
over this part of the motd. thanks.
\_ the first "1" in 1d4+1 is redundant. unless you're using it as
a placeholder, remove it, or you threaten a segfault in the rest
of us.
\_ No, that's how it's done. When you publish your own game
system you can drop the "1" in front. Until then you take
1d30+7 silliness points and are stunned 1d4+1 motd threads. |