const c structure question

Discussion in 'C Programming' started by nospam, Jun 5, 2007.

  1. nospam

    nospam Guest

    Hi all,

    I have this struct typedef:

    typedef struct
    {
    int * test;
    }testst;

    and have this declaration:

    const testst abc;

    What is constant? The pointer (like in: int * const test) or the integer
    (like in const int * test) or both?

    Thanx,

    Milux
     
    nospam, Jun 5, 2007
    #1
    1. Advertisements

  2. nospam

    Mike Wahler Guest

    "nospam" <""miluxkloosterman\"@(nospam)home.nl"> wrote in message
    news:f446g0$qm5$...
    > Hi all,
    >
    > I have this struct typedef:
    >
    > typedef struct
    > {
    > int * test;
    > }testst;
    >
    > and have this declaration:
    >
    > const testst abc;
    >
    > What is constant?


    The object named 'abc'.

    > The pointer (like in: int * const test) or the integer (like in const int
    > * test) or both?


    The entire object is const. Which means that each
    individual member of the struct is const. Your struct
    contains only one member, a pointer (type 'int*'), there
    is no type 'int' member. So it's not a question of 'one
    of or both', since that presumes two members. The pointer
    'test' is const. What it points to (if anything) is not
    const. A pointer to a const int is declared:

    const int *p;

    or

    int const *p;


    What specifically are you trying to do? Write
    code for a particular task, or understand something
    written by someone else?

    -Mike
     
    Mike Wahler, Jun 5, 2007
    #2
    1. Advertisements

  3. nospam

    Alan Curry Guest

    In article <f446g0$qm5$>,
    nospam <""miluxkloosterman\"@(nospam)home.nl"> wrote:
    >Hi all,
    >
    >I have this struct typedef:
    >
    >typedef struct
    >{
    > int * test;
    >}testst;


    For the purposes of exploring constness, the struct is unnecessary
    obfuscation. The question would apply just as well to

    typedef int *testp;

    >
    >and have this declaration:
    >
    >const testst abc;


    const testp abcde;

    >
    >What is constant? The pointer (like in: int * const test) or the integer
    >(like in const int * test) or both?


    abcde is a const pointer to non-const int.
    abc.test is also a const pointer to non-const int.

    --
    Alan Curry
     
    Alan Curry, Jun 5, 2007
    #3
  4. nospam

    Milux Guest

    > ...
    > The object named 'abc'.
    > ...
    > The entire object is const. Which means that each
    > individual member of the struct is const. Your struct
    > contains only one member, a pointer (type 'int*'), there
    > is no type 'int' member. So it's not a question of 'one
    > of or both', since that presumes two members. The pointer
    > 'test' is const. What it points to (if anything) is not
    > const. A pointer to a const int is declared:
    >
    > const int *p;
    >
    > or
    >
    > int const *p;
    >
    >
    > What specifically are you trying to do? Write
    > code for a particular task, or understand something
    > written by someone else?
    >

    I was exploring this because the structure contains variables declared
    volatile. Thus
    volatile int * const test;
    But because the programm was not working correctly, I thought it had to do
    with the object declared const. But I understand you explanation so this
    part was correct, thanx.

    > -Mike
    >
    >
     
    Milux, Jun 5, 2007
    #4
  5. [restored attributions]

    Milux wrote:
    > Mike Wahler wrote:
    >>
    >> What specifically are you trying to do? Write
    >> code for a particular task, or understand something
    >> written by someone else?
    >>

    > I was exploring this because the structure contains variables declared
    > volatile. Thus
    > volatile int * const test;
    > But because the programm was not working correctly, I thought it had
    > to do with the object declared const. But I understand you explanation
    > so this part was correct, thanx.


    While exploring this, you should be aware that 'const' is actually very
    badly named.
    The effect of const-qualifying something is only to mark it as
    read-only. It does not make it constant/unchangeable.

    For example, on machines that allow direct access to (memory mapped)
    hardware, you could have something like this
    const volatile int * const current_time = (const volatile int*)0x1234;
    to gain access to a hardware timer.
    Through the variable current_time, you are allowed to read the current
    value of the timer, but you can not modify it from within the program.
    Obviously, the value you read will differ over time.

    >
    >> -Mike
    >>
    >>

    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
     
    Bart van Ingen Schenau, Jun 7, 2007
    #5
    1. Advertisements

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. Dario de Judicibus

    static const array of const structure

    Dario de Judicibus, Nov 10, 2003, in forum: C++
    Replies:
    1
    Views:
    9,185
    Rolf Magnus
    Nov 10, 2003
  2. Replies:
    11
    Views:
    1,384
  3. Javier
    Replies:
    2
    Views:
    875
    James Kanze
    Sep 4, 2007
  4. linq936
    Replies:
    2
    Views:
    450
    Richard Bos
    Sep 18, 2007
  5. Adem24
    Replies:
    8
    Views:
    451
    Bo Persson
    Jun 14, 2008
  6. 0m
    Replies:
    26
    Views:
    1,467
    Tim Rentsch
    Nov 10, 2008
  7. fungus
    Replies:
    13
    Views:
    1,206
    fungus
    Oct 31, 2008
  8. Replies:
    2
    Views:
    680
    Andrew Koenig
    Feb 9, 2009
Loading...