0

I have the following code snippet:

#include<stdio.h>
void read(int a[ ],int n)
{
      static int p=n;
      if(n!=0)
       {
           printf("enter element %d: ",p-n);
           scanf("%d",&a[p-n]);
           read(a,n-1);
        }
}
int main()
{
   int a[10],n;
   printf("enter n: ");
   scanf("%d",&n);
   read(a,n);
}

I keep getting the error: initializer element is not constant. Isn't n constant by the time the function compiles?

Edit: Problem: How to set the value of a static variable(if it isn't set) within a function?

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
Ali
  • 406
  • 5
  • 15
  • 3
    It is because you can't initialize a static variable with another variable, only constant values that can be determined at compile time. – ddz Apr 23 '16 at 20:20
  • 2
    The answer is no. It's not constant, is a variable, passed to a function. In C you can have a compile time constant by using `#define n 10`. Also, there are several post on this argument, see for example http://stackoverflow.com/questions/3025050/error-initializer-element-is-not-constant-when-trying-to-initialize-variable-w – terence hill Apr 23 '16 at 21:11
  • 1
    `static int p=n;` --> `static int p; if(!p) p = n;` – BLUEPIXY Apr 24 '16 at 00:19
  • Thanks guys, I solved it BLUEPIXY's way. – Ali Apr 24 '16 at 06:24

4 Answers4

4

It is because you can't initialize a static variable with another variable, only with constant values that can be determined at compile time, such as macros, literals, etc.

ddz
  • 526
  • 3
  • 15
2

Most certainly not; how does the compiler know what value is going to be assigned to n at compile time?

Cyb3rFly3r
  • 1,321
  • 7
  • 12
2

I keep getting the error: initializer element is not constant.

Global and static variables can only be initialized with constant expressions known at compile time.

Isn't n constant by the time the function compiles?

The answer is no. The n stores value received from stdin. Therefore, it receives the value during run-time.

abhiarora
  • 9,743
  • 5
  • 32
  • 57
1

Edit : Solution is assign p to n only if p is unset

Solution:

#include<stdio.h>
static int p;
void read(int a[ ],int n)
{
    //p=n; //to change n on each call 
    if(!p) p = n; //to change n only if p is unset
      if(n!=0)
       {
           printf("enter element %d: ",p-n);
           scanf("%d",&a[p-n]);
           read(a,n-1);
        }
}
int main()
{
    int a[10],n;
    printf("enter n: ");
    scanf("%d",&n);
    read(a,n);
}
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • Thanks for your help, but that was not what I needed, because p takes a new n everycall. – Ali Apr 24 '16 at 06:24
  • What exactly is the issue? Don't you want p to get n value each loop? – Ani Menon Apr 24 '16 at 06:51
  • Nope, I wanted it to take only the first n, so p-n is used as index for the array. – Ali Apr 24 '16 at 07:10
  • @user6019827 That wasn't clear in the question. Edited the answer. Is that what you were looking for? – Ani Menon Apr 24 '16 at 07:13
  • @user6019827 also read [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) – Ani Menon Apr 24 '16 at 07:44
  • The original version of the question had capital letters at the start of most of the lines — probably due to a tablet or phone doing case-conversion. The reported error message, though, is not consistent with having problems with case of letters. – Jonathan Leffler Apr 24 '16 at 07:58
  • @JonathanLeffler the question was unclear earlier. Now the problem statement was edited & accordingly the code above works. – Ani Menon Apr 24 '16 at 08:00
  • I agree the original was unclear; my comment was documenting to those who come after why you don't deserve down-votes for what is no longer a visible problem (because the question has been edited). However, your diagnosis was not consistent with the compiler error message. – Jonathan Leffler Apr 24 '16 at 08:07
  • @JonathanLeffler The answer has been edited accordingly now. – Ani Menon Apr 24 '16 at 08:08
  • Your code has changed the location of `p` (it's no longer hidden in a function) which could affect the behaviour of other code — if there was any. It also would make it possible to use the function for more than loading a single array (because you could reset `p` from another function); the existing design doesn't allow that. I would need to spend time studying the code more to say more; but it's an hour or more after bedtime here and the code isn't sufficiently exciting to warrant the effort. – Jonathan Leffler Apr 24 '16 at 08:08
  • I believe in answering to the question asked and the current answer is what the user wanted. – Ani Menon Apr 24 '16 at 08:11