0

How to declare static variable prior to its definition? The use case is there is other global variable is using it before it is defined. And I don't want to move the definition to top.

Example code:

extern static int a; //compiler error, but I need to declare 'a' because it is used below by 'x'

typedef struct{
 int * dd;
}X;

static X x={&a}; //this variable needs to use 'a'

static int a=5; //this is where 'a' defined

Above code is compile error.

=== Update ====

Well, I found the solution myself. Just remove the extern keyword.

dragon135
  • 1,366
  • 8
  • 19
  • 1
    any reason can't reorder `static int a;` to top? – Bryan Chen Jun 04 '14 at 04:07
  • 2
    But why don't you just declare `static int a` at the top of your source file, and assign it later ? What would be the issue ? – quantdev Jun 04 '14 at 04:07
  • possible duplicate of [Static variable](http://stackoverflow.com/questions/1973162/static-variable) – Spundun Jun 04 '14 at 04:14
  • 1
    Moderators, please ignore my flag for duplicate... On a second thought I don't think it's a duplicate of http://stackoverflow.com/questions/1973162/static-variable – Spundun Jun 04 '14 at 04:20
  • I want to know how it is done or if it is possible in C. It looks like a flaw if we cannot declare static as extern. What is the reason for this. I don't see problem if extern static is allowed. – dragon135 Jun 04 '14 at 04:30
  • @Spundun - you should be able to cancel the flag by clicking on "close" again. – Ganesh Sittampalam Jun 04 '14 at 13:32
  • @GaneshSittampalam If I click flag again... it says "you have already raised this flag" and I don't see any close button. It's probably a privilege thing. – Spundun Jun 04 '14 at 14:01

3 Answers3

2

You are trying to use two storage classes at a time. Thats problematic. Use static int a;, and you can access it in your file, just make sure you are defining it above the code you are using it.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
  • What is the reason extern static is forbidden? I think the reason is just because keyword extern and static is same type (storage classes), just like 'float int' is illegal. But I think 'extern static' is logical (it says there is static variable somewhere below in this file). – dragon135 Jun 04 '14 at 04:34
  • 1
    @dragon135 no, `extern` doesn't mean that. It means that "theres a variable named `a` in *some* file". And thats contradictory with `static` (which means "you can only access this variable from within this file"). – The Paramagnetic Croissant Jun 04 '14 at 04:47
  • @user3477950 it is not difficult to make additional rule so that it become like this: when compiler see 'extern' it means "there's a variable named a somewhere in some file" then after seeing keyword static it deduce "there's a variable named a somewhere in some file, and the file is actually this file". – dragon135 Jun 04 '14 at 07:13
1

Read http://www.learncpp.com/cpp-tutorial/43-file-scope-and-the-static-keyword/

In your example, the variable is declared static in the file scope. This means that it's available to all the code in the file.

In such a case it doesn't make sense to define it below the code that it uses. You should simply move it to the top of the file.

Also checkout Forward declaring static C struct instances in C++ if you have a need to do this for some reason. May be the solution provided there could be adapted to fit your larger goal.

Community
  • 1
  • 1
Spundun
  • 3,936
  • 2
  • 23
  • 36
0

You cannot have two storage classes used on one single variable. Doing this will flag an error

ashutosh
  • 79
  • 3
  • 8