Create readonly array of data ?

Discussion in 'C Programming' started by paolo, Jan 2, 2012.

  1. paolo

    paolo Guest

    Please excuse my very poor English, I hope you understand my question.

    I want to create a readonly array of data, then a readonly array of a
    structure (STRUCT). This is data I access but I want it protected against
    accidental change. The following is my test code.

    #include "stdafx.h"

    struct LVC
    {
    unsigned short int lo;
    unsigned short int hi;

    };

    void main()
    {
    //This seems to work
    static const unsigned short int LV[4] =
    {0xAC00,
    0xAC1C,
    0xAC38,
    0xAC54 };

    //THIS DOESN'T WORK. COMPILER COMPLAINS
    static const struct LVC[4] = {
    { 0xAC01, 0xAC1B },
    { 0xAC1D, 0xAC37 },
    { 0xAC39, 0xAC53 },
    { 0xAC55, 0xAC6F },
    };

    unsigned short i,j;

    i = LVC[2].lo;
    j = LVC[2].hi;

    }

    I use Microsoft Visual C++ Express Edition
    (I also would like to get rid of that #include "stdafx.h" if
    there is some compiler configuration that will allow me
    to do that.)

    Thank you.
    paolo, Jan 2, 2012
    #1
    1. Advertising

  2. paolo

    Eric Sosman Guest

    On 1/2/2012 4:44 PM, paolo wrote:
    > Please excuse my very poor English, I hope you understand my question.
    >
    > I want to create a readonly array of data, then a readonly array of a
    > structure (STRUCT). This is data I access but I want it protected against
    > accidental change. The following is my test code.
    >
    > #include "stdafx.h"
    >
    > struct LVC
    > {
    > unsigned short int lo;
    > unsigned short int hi;
    >
    > };
    >
    > void main()


    int main(void)

    > {
    > //This seems to work
    > static const unsigned short int LV[4] =
    > {0xAC00,
    > 0xAC1C,
    > 0xAC38,
    > 0xAC54 };
    >
    > //THIS DOESN'T WORK. COMPILER COMPLAINS
    > static const struct LVC[4] = {


    This array needs a name. `LVC' is not the name of anything
    at all; `struct LVC' is the name of a type, just like `float' is
    the name of a type. Try something like

    static const struct LVC array[4] = { ...

    > { 0xAC01, 0xAC1B },
    > { 0xAC1D, 0xAC37 },
    > { 0xAC39, 0xAC53 },
    > { 0xAC55, 0xAC6F },
    > };
    >
    > unsigned short i,j;
    >
    > i = LVC[2].lo;
    > j = LVC[2].hi;


    .... and `i = array[2].lo; j = array[2].hi;'.

    > }
    >
    > I use Microsoft Visual C++ Express Edition
    > (I also would like to get rid of that #include "stdafx.h" if
    > there is some compiler configuration that will allow me
    > to do that.)


    Just leave it out: It's not part of C, and no C compiler needs
    it. If your compiler complains when you omit "stdafx.h", it is
    operating in some sort-of-like-C-but-not-actually-C mode; there are
    probably compiler flags or options to tell it to behave like a C
    compiler instead. (I'm sorry I don't know what those options might
    be; perhaps a Microsoft forum would be able to help.)

    --
    Eric Sosman
    d
    Eric Sosman, Jan 2, 2012
    #2
    1. Advertising

  3. paolo

    ec429 Guest

    On 02/01/12 21:44, paolo wrote:
    > //THIS DOESN'T WORK. COMPILER COMPLAINS

    The declaration "static const struct LVC[4]" is invalid; you have not
    given the variable a name. "struct LVC" is a type, not a declaration.
    Try something like "static const struct LVC my_lvc[4]" instead.

    Also, some advice: when asking for help, post the actual error message
    produced by the compiler; it makes it easier for others to help you and
    thus more likely that they will do so.

    > (I also would like to get rid of that #include "stdafx.h" if
    > there is some compiler configuration that will allow me
    > to do that.)

    According to a quick web search, stdafx.h is a precompiled header
    containing "standard system and project specific include files that are
    used frequently but hardly ever change". You could replace it with
    #includes of those headers you actually need, though you may need to
    disable the compiler option /Yu'stdafx.h'
    -E
    --
    'sane', adj.: see 'unimaginative'
    on the web - http://jttlov.no-ip.org
    ec429, Jan 2, 2012
    #3
  4. paolo <> wrote:
    > Please excuse my very poor English, I hope you understand my question.


    > I want to create a readonly array of data, then a readonly array of a
    > structure (STRUCT). This is data I access but I want it protected against
    > accidental change. The following is my test code.


    > #include "stdafx.h"


    > struct LVC
    > {
    > unsigned short int lo;
    > unsigned short int hi;


    > };


    > void main()


    That should be

    int main( void )

    (main() is supposed to retur an int and take either no arguments
    or an int and an array of char pointers, but not an unsoecified
    number of arguments).

    > {
    > //This seems to work
    > static const unsigned short int LV[4] =
    > {0xAC00,cd TE


    > 0xAC1C,
    > 0xAC38,
    > 0xAC54 };


    What is the 'static' meant to be good for? Also without
    it this will create an array that is read-only. 'static'
    only is necessary if you want a variable that "survives"
    the end of the function and thus still exists (with the
    previous value) when you call the function another time.

    > //THIS DOESN'T WORK. COMPILER COMPLAINS
    > static const struct LVC[4] = {


    The problem is that this should be something like

    const struct LVC some_name[ 4 ] = {

    and then the compiler should accept it. That's because
    the type is 'struct LVC' and you're creating a strange
    mix out of the type name and the variable name.

    > { 0xAC01, 0xAC1B },
    > { 0xAC1D, 0xAC37 },
    > { 0xAC39, 0xAC53 },
    > { 0xAC55, 0xAC6F },
    > };



    > I use Microsoft Visual C++ Express Edition


    I hope you use it as a C and not a C++ compiler, otherwise
    you'd better ask in comp.lang.c++.

    > (I also would like to get rid of that #include "stdafx.h" if
    > there is some compiler configuration that will allow me
    > to do that.)


    There's nothing in the program you psoted that would require
    the inclusion of any header files.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jan 2, 2012
    #4
  5. paolo

    Thad Smith Guest

    On 1/2/2012 4:12 PM, Jens Thoms Toerring wrote:
    > paolo<> wrote:
    >> Please excuse my very poor English, I hope you understand my question.

    >

    ....
    >> {
    >> //This seems to work
    >> static const unsigned short int LV[4] =
    >> {0xAC00,cd TE

    >
    >> 0xAC1C,
    >> 0xAC38,
    >> 0xAC54 };

    >
    > What is the 'static' meant to be good for? Also without
    > it this will create an array that is read-only. 'static'
    > only is necessary if you want a variable that "survives"
    > the end of the function and thus still exists (with the
    > previous value) when you call the function another time.


    While that is true, it is also more efficient for many implementations,
    especially embedded implementations which use both read-only and read-write
    storage. With the static specifier, the data can be loaded into read-only
    memory, while without the static attribute, most implementations will copy the
    data into read-write memory at run time, increasing code size (slightly), run
    time on each function entry, and RAM requirements. The latter can be
    significant on small processors.

    Thad
    Thad Smith, Jan 3, 2012
    #5
  6. paolo wrote:

    >I use Microsoft Visual C++ Express Edition
    >(I also would like to get rid of that #include "stdafx.h" if
    >there is some compiler configuration that will allow me
    >to do that.)


    Disable "use precompiled headers"
    --
    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]
    Roberto Waltman, Jan 4, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jeff h. silverman
    Replies:
    1
    Views:
    301
    Roedy Green
    Jul 28, 2003
  2. Susan Rice

    Create readonly array of data?

    Susan Rice, Jan 1, 2007, in forum: C Programming
    Replies:
    14
    Views:
    552
    rhle.freak
    Jan 3, 2007
  3. Replies:
    7
    Views:
    397
  4. Replies:
    0
    Views:
    350
  5. Jonathan Hyatt
    Replies:
    1
    Views:
    1,899
    Jonathan Hyatt
    Jun 8, 2004
Loading...

Share This Page