static variable

This is a discussion on static variable within the C Programming forums, part of the General Programming Boards category; I want to declare a static variable in function. And its value needs to be assigned first time only. But ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    static variable

    I want to declare a static variable in function. And its value needs to be assigned first time only.
    But the value that it needs to be assigned is also a variable. Force assigning a variable to static variable gave compiler error. Any suggestion to do the same by some alternative means.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    There is nothing wrong with doing that. Post the copiler error or code and we'll work that out.

    gg

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Code:
    ID is global variable
    
    BYTE push()
    {
    	char aa[2000];
    	static BYTE returnVal = 0;
    	static BYTE element = (ID<<5); // needs to be assigned
    	
    .
    }
    After compilation
    
    helper.c(210) : error C2099: initializer is not a constant
    Error executing cl.exe.
    
    helper.obj - 1 error(s), 0 warning(s)

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This shows the problem.
    Code:
    #include <stdio.h>
    
    void foo(int i)
    {
      static int j = i;
    }
    
    int main(void)
    {
      foo(10);
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>static BYTE element = (ID<<5);
    The value of the static variable must be const (as your compiler says).

    Code:
    ISO/IEC 9899:1999 (E)
    
    6.7.8 Initialization
    
    4 All the expressions in an initializer for an object 
    that has static storage duration shall be constant 
    expressions or string literals.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Any alternative idea, without writing much code.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Roaring_Tiger
    Any alternative idea, without writing much code.
    That's really going to depend on how you design your program and what it's actually supposed to be doing.

    How about this:
    Code:
    #include <stdio.h>
    
    void foo(int accountNum)
    {
      static int Prev_accountNum;  /* init to 0 by default */
      
      printf ("This time: %d\nLast time: %d\n", accountNum, Prev_accountNum);
      
      Prev_accountNum = accountNum;
    }
    
    int main(void)
    {
      foo(10);
      foo(20);
      foo(30);
      return 0;
    }
    
    /*
     * Output
     This time: 10
     Last time: 0
     This time: 20
     Last time: 10
     This time: 30
     Last time: 20
     *
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    Code:
    BYTE push()
    {
    	char aa[2000];
    	static BYTE returnVal = 0;
    	static BYTE element; // needs to be assigned
    BYTE 	firstTime=1;
    
    If (firstTime)
    {
         element =  = (ID<<5);
         firstTIme = 0;
    }
    while (  !returnVal)
    {
    original code
    }
    .
    }

    Any suggestion or better idea than...

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What does "element" mean to the program?

    Why do you need it to be static (ie remember its value between function calls)?

    Why is its initial value (ID<<5) ?

    >>while ( !returnVal)
    The initial value of returnVal is 0 so this section of code will never run.

    Why is returnVal static?

    Think about your need for static variables, use them sparingly.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,671
    Originally posted by Codeplug
    There is nothing wrong with doing that...
    In C++ (doh!)

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  2. Static variable usage
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 12:33 AM
  3. static variable vs. DLL
    By pheres in forum C++ Programming
    Replies: 11
    Last Post: 02-06-2008, 01:15 AM
  4. what is static variable....?
    By gardenair in forum C Programming
    Replies: 4
    Last Post: 04-09-2003, 08:54 AM
  5. Replies: 3
    Last Post: 10-10-2002, 07:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21