Re: static function, not a member?

From: JKop (NULL_at_NULL.NULL)
Date: 06/21/04


Date: Mon, 21 Jun 2004 15:29:03 GMT

Howard posted:

> Hi,
>
> I came across some sample code for using a third-party API, and the
> code
> in it had several free-standing (i.e., non-member) functions, but
> strangely, these were declared as 'static'. My compiler tells me that
> the function has no prototype, which is weird, because there *is* a
> prototype in the header file. I was not able to find in my books any
> reference to a static function that was not a member function. The
> closest info I could find (via google) was something about making the
> symbol "local", and a suggestion that this was deprecated in C++.
>
> Can someone tell me what it means to have the static keywoard in
> front
> of a non-member function, and whether it can be safely removed for my
> use?
>
> Thanks,
> Howard

EXAMPLE 1

void Horse(void)
{
            static int j = 7;

            j +=2 ;
}

int main(void)
{
            Horse();
            Horse();
            Horse();
            Horse();
            Horse();

            // The variable j is global, but can only be accessed from
            // within the Horse function. Right now, it's value is 17.
}

EXAMPLE 2

static int hello = 45;

int main(void)
{
            ;
}

The global variable hello cannot be accessed from outside the current CPP
file.



Relevant Pages

  • Re: Undefined reference
    ... static int square ... I don't understand why I'm getting the undefined reference error. ... I'm using the Dev compiler and it seems that it is ANSI-compatible. ... The declarations in the header file only tell the compiler how ...
    (comp.lang.c)
  • Undefined reference
    ... static int square ... I'm using the Dev compiler and it seems that it is ANSI-compatible. ... The declarations in the header file only tell the compiler how ... to call the external functions; the header file doesn't supply the ...
    (comp.lang.c)
  • Re: accessing variables from more than one .cpp file
    ... i want to declare some variables with in a header file. ... > say int i; ... The compiler will create an executable for you. ...
    (comp.lang.cpp)
  • Re: integer and floating-point bit pattern
    ... > to include the proper header file for any ... Prior to the current standard you did not need a prototype or a ... declaration to call a function that returned int and accepted a fixed ...
    (comp.lang.c)
  • Re: [PATCH -mm 0/3] sysv ipc: increase msgmnb with the number of cpus
    ... recompute_msgmnbto a header file, so we have a window in which the ... we don't want such tunables to be recomputed anymore upon memory ... switch { ... extern int ipcns_notify; ...
    (Linux-Kernel)