tags:

views:

84

answers:

2

I have a problem regarding static variables. It is said that the life of the static variable is beyond the bounds of the function (if defined in a function). But a pointer to it must give the value if it exits. But it does not work.

#include<stdio.h>
int *p;
int main()
{
    clrscr();
    test();
    printf("%d",*p);
    return 0;
}

void test(void)
{
    static int chacha=0;
    p=&chacha;
}
+3  A: 

It doesn't look like you declared p anywhere.

Try this in test:

int* test(void)
{
    static int chacha = 0;
    return &chacha;
}

Now if your main is:

int main()
{
    int *p;
    clrscr();
    p = test();
    printf("%d",*p);
    getch();
    return 0;
}

you'll see the behavior you expect.

Nathan Fellman
+2  A: 
int *p;

int main()
. . .
. . .
. . .
p = &chacha;
DigitalRoss