static pointer

Discussion in 'C Programming' started by boogie_down77@yahoo.com, Mar 2, 2005.

  1. Guest

    Is it possible to declare and initialize a static node pointer in C? I
    pass a node pointer in a function and I want to declare and initialize
    a static node pointer and initialize it to the head of the linked list.
    The last thing I do (or should I say, want to do)is to increment the
    static pointer. However I keep getting a compile error which complains
    that the initilizing value must be constant. How could this be? I
    don't get an error when I simply declare: static NODEP static_pointer;
    Can you not initialize a static pointer in C?

    void function ( NODEPP linked_list )
    {
    static NODEP static_pointer; /* OK */
    }

    void function ( NODEPP linked_list )
    {
    static NODEP static_pointer = (*linked_list); /* not ok, why */
    }
    , Mar 2, 2005
    #1
    1. Advertising

  2. Artie Gold Guest

    wrote:
    > Is it possible to declare and initialize a static node pointer in C? I
    > pass a node pointer in a function and I want to declare and initialize
    > a static node pointer and initialize it to the head of the linked list.
    > The last thing I do (or should I say, want to do)is to increment the
    > static pointer. However I keep getting a compile error which complains
    > that the initilizing value must be constant. How could this be? I
    > don't get an error when I simply declare: static NODEP static_pointer;
    > Can you not initialize a static pointer in C?
    >
    > void function ( NODEPP linked_list )
    > {
    > static NODEP static_pointer; /* OK */
    > }
    >
    > void function ( NODEPP linked_list )
    > {
    > static NODEP static_pointer = (*linked_list); /* not ok, why */
    > }
    >

    It's exactly as the error says: When you initialize a static, it must be
    initialized with a constant.

    What are you trying to accomplish in your code?

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    Artie Gold, Mar 2, 2005
    #2
    1. Advertising

  3. writes:
    > Is it possible to declare and initialize a static node pointer in C? I
    > pass a node pointer in a function and I want to declare and initialize
    > a static node pointer and initialize it to the head of the linked list.
    > The last thing I do (or should I say, want to do)is to increment the
    > static pointer. However I keep getting a compile error which complains
    > that the initilizing value must be constant. How could this be? I
    > don't get an error when I simply declare: static NODEP static_pointer;
    > Can you not initialize a static pointer in C?
    >
    > void function ( NODEPP linked_list )
    > {
    > static NODEP static_pointer; /* OK */
    > }
    >
    > void function ( NODEPP linked_list )
    > {
    > static NODEP static_pointer = (*linked_list); /* not ok, why */
    > }


    As Artie Gold already mentioned, the error message is exactly correct;
    the initialization for a static object must be constant, and a
    reference to a function parameter isn't constant.

    The linked_list parameter has a new value every time the function is
    called, so it looks like you want to initialize static_pointer on
    every call -- but it's static, which means its lifetime extends beyond
    any single call. If that's really what you want to do, you can
    use an assignment rather than an initializer:

    void function ( NODEPP linked_list )
    {
    static NODEP static_pointer;
    static_pointer = *linked_list;
    }

    but then why not just make it a non-static local variable? The only
    reason to make it static is so it maintains its value across calls.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Mar 2, 2005
    #3
  4. On Tue, 01 Mar 2005 18:05:55 -0800, boogie_down77 wrote:

    > Is it possible to declare and initialize a static node pointer in C? I
    > pass a node pointer in a function and I want to declare and initialize
    > a static node pointer and initialize it to the head of the linked list.
    > The last thing I do (or should I say, want to do)is to increment the
    > static pointer. However I keep getting a compile error which complains
    > that the initilizing value must be constant. How could this be? I
    > don't get an error when I simply declare: static NODEP static_pointer;
    > Can you not initialize a static pointer in C?
    >
    > void function ( NODEPP linked_list )
    > {
    > static NODEP static_pointer; /* OK */
    > }
    >
    > void function ( NODEPP linked_list )
    > {
    > static NODEP static_pointer = (*linked_list); /* not ok, why */
    > }


    Static variables are created and initialised once at program startup and
    continue to exist right until the program terminates. In this case
    linked_list doesn't exist at the point where static_pointer is
    initialised (i.e. before main() is called). This is why initialisers for
    static objects are required to be constant expressions, the compiler must
    be able to evaluate them without having to execute program statements.

    Lawrence
    Lawrence Kirby, Mar 2, 2005
    #4
    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. gorda
    Replies:
    5
    Views:
    5,031
    akhyls
    Mar 7, 2010
  2. Replies:
    10
    Views:
    676
    Chris Torek
    Feb 4, 2005
  3. jimjim
    Replies:
    16
    Views:
    827
    Jordan Abel
    Mar 28, 2006
  4. Replies:
    4
    Views:
    1,234
    Fred Zwarts
    Jul 2, 2009
  5. A
    Replies:
    7
    Views:
    626
Loading...

Share This Page