1

What I thought static pointer is like other static variables, ones initialised with an value it have same value till end, like that the same address will be held in the static pointer. But in this case the compiler is throwing error

//initialiser element is not constant      static int *a = &b[0];

#include <stdio.h>
int main(void) 
{
    int b[2];

    static int *a = &b[0];  // removing static the program works well.
    printf("%u",a);

    a = &b[1];
    printf("%u",a);

    return 0;
}

So what is the use of static pointer?

Aswin Prasad
  • 407
  • 6
  • 14
  • Is that supposed to be C or C++? They are different languages, pick **one**! – too honest for this site Oct 20 '16 at 13:38
  • @Olaf: This question is interesting with C and C++ tags set: the answer is very different. – Bathsheba Oct 20 '16 at 13:39
  • 1
    @Bathsheba: That's the reason it should have one language tag only! Otherwise it would be too broad. But it can be asked twice, once for each language. – too honest for this site Oct 20 '16 at 13:44
  • If you search "initializer not constant" on SO, you might find that this question has been asked many times before. http://stackoverflow.com/search?q=%5Bc%5D+initializer+not+constant – Lundin Oct 20 '16 at 13:45
  • @Olaf: I'm never too sure about that; particularly if the question has an implicit "difference between C and C++" edge to it. I wonder if an expert will confirm my second paragraph? – Bathsheba Oct 20 '16 at 13:46
  • Also my issue is with pointers @Lundin and not the constant property. There are no obvious questions about static pointers anywhere, So kindly release the duplicate – Aswin Prasad Oct 20 '16 at 13:47
  • @aswinmythili The `static` part is the same for any kind of variable. The problem is that variables of _static storage duration_ must have a constant expression as initializer. As for "ones initialised with an value it have same value till end", that's not what static variables do, you seem to confuse them with `const`. – Lundin Oct 20 '16 at 13:49
  • Got it now @olaf, Const and static differences are OK, And i understood finally that static pointer variable should pointer to one of constant variable or static variable, and not automatic variables, which i tried in the above. So nice explanation . Correct me if i'm wrong. – Aswin Prasad Oct 20 '16 at 13:53
  • @aswinmythili: A pointer is a first-class variable. So why should it behave differently? – too honest for this site Oct 20 '16 at 13:55
  • 1
    @aswinmythili: Why that differentiation? A static (which is not just `static` _qualified_ variables, but any at file-scope) variable needs a _constant-expression_ initialiser **point**. And that's exactly what the error message tells! – too honest for this site Oct 20 '16 at 14:15

3 Answers3

4

In C, your code doesn't make sense. b has automatic storage duration so conceptually will have a different address each time main is encountered. The static will be initialised only once, and on subsequent invocations of main it may well point to something invalid.

But, and this is the interesting bit, in C++ it ought to make sense since you are not allowed to call main yourself: the behaviour on your doing so is undefined. So the inference of this is that the compiler ought to know that the static is valid for the lifetime of main, and compile the code! Perhaps there is something in the C++ Standard that explicitly forbids this.

In C you are allowed to call main recursively (even implicitly recursively), so the compiler ought to emit an error.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • What is static pointer then, is it only for static variables?, Consider that i don't call main recursively. – Aswin Prasad Oct 20 '16 at 13:37
  • 1
    A pointer with `static` storage duration will be initialised once. In this context, it would only really make sense if it were pointing to a variable with `static` storage duration. – Bathsheba Oct 20 '16 at 13:43
  • I disagree with the "ought to" part. It would be special treatment for an otherwise automatic variable. Also the initialisation of static variables is done _before_ `main` is called, the the address of any automatic variable is not know at compile-time. – too honest for this site Oct 20 '16 at 13:47
  • 1
    In C++ the initialisation of `static` variables at function scope happens conceptually when program control first encounters them (although a compiler is allowed to re-order that so long as there are no side effects). Are the rules for C different? – Bathsheba Oct 20 '16 at 13:48
  • @Bathsheba: 1) OP asks about static variables, not `static` qualified only. 2) IIRC the standard requires all static variables (which includes `static` qualified) to be initialised on entry to `main`. I wounder that is different in C++ actually, as that would change the semantics for C++ objects compared to C. IIRC, gcc generates a list of static initialisers which is run from the startup code. But then I'm not sure this is just for global variables. Anyway; the C standard is clear about requiring constant expressions as initialisers, which an auto var is not. – too honest for this site Oct 20 '16 at 13:54
3

You have two options. Add static to int b[2], or remove it from int *a. The address of b isn't static. It's variable, because b is a variable with automatic storage.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • What is static pointer then, is it only for static variables? – Aswin Prasad Oct 20 '16 at 13:43
  • @aswinmythili: The error message is very clear. Any static variable, which includes global variables, need to have a constant expression as initialiser. It becomes clear why if you have understood what static vriables actually are. – too honest for this site Oct 20 '16 at 13:49
0

There may be confusion about static vs const.

Const variables will keep the same value from the time they are initialized until they go out of scope, unless const_cast<> is used, though as @Bathsheba mentioned in comment, use of const_cast<> on a variable declared const is undefined.

Static means it will get initialized at the spot first reaches but then not go out of scope until the end of program execution.

Tim Beaudet
  • 791
  • 7
  • 16