|  |  | 
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: creating an (external) global variable containing a structure
Suppose I want to create a global variable containing a structure  
from within a function.  For example:
struct mystruct {
    real scalar    a
}
void create_mystruct()
{
    external struct mystruct scalar    foo
}
I would have assumed that after calling create_mystruct(), foo would  
contain a struct scalar, just as though I had used the constructor  
mystruct().  Instead, however, it contains a (NULL) pointer scalar:
. mata: create_mystruct()
. mata: mata describe
      # bytes   type                        name and extent
------------------------------------------------------------------------ 
-------
           52   void                        create_mystruct()
           68   structdef scalar            mystruct()
            8   pointer scalar              foo
------------------------------------------------------------------------ 
-------
. mata: foo
  0x0
This puzzled me, because it seems a bit inconsistent with the way  
external declarations work for other types.  Moreover, I'm not sure  
what the resulting object is intended to be used for.  For example,  
the following will not compile:
void create_mystruct()
{
    external struct mystruct scalar    foo
    struct mystruct scalar             foobar
    foobar = mystruct()
    foo = &foobar  // this line yields a type mismatch error
}
which demonstrates that the line
    external struct mystruct scalar    foo
does not yield the same result as
    external pointer(struct mystruct scalar) scalar    foo
does, even though they both create pointer scalars.  So, I guess I  
really have a two-part question: First, how does the compiler  
interpret the original declaration of foo, and, how should one create  
an (external) global variable containing a structure from within a  
function?
-- Phil
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/