static pointer

B

boogie_down77

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 */
}
 
A

Artie Gold

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
 
K

Keith Thompson

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.
 
L

Lawrence Kirby

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top