typedef & static

This is a discussion on typedef & static within the C Programming forums, part of the General Programming Boards category; here's the code... Code: #include <stdio.h> struct foo { int n; float k; }; /*static struct foo { int n; ...

  1. #1
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46

    typedef & static

    here's the code...

    Code:
    #include <stdio.h>
    
    struct foo {
    	int n;
    	float k;
    };
    
    /*static struct foo {
    	int n;
    	float k;
    };*/
    
    typedef struct foo2 {
    	int m;
    	float l;
    } Foo2;
    
    /*typedef static struct foo2 { // this is wrong
    	int m;
    	float l;
    } Foo2;*/
    
    typedef struct foo2 Foo2; /* this is correct */
    
    int main( void )
    {
    	return 0;
    }
    why the declaration of foo2 with both static and typedef
    specifiers is not right?

    thanks in advance,
    trk
    to boldy code where...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    /*static struct foo {
    	int n;
    	float k;
    };*/
    This is incorrect because static only specifies that an instance of a variable, or a given function, is static. In the above example, you are not creating an instance of a variable, so you cannot declare it static (because you have nothing to declare).

    The same reason applies why you couldn't use 'const' here (or automatic).

    Code:
    /*typedef static struct foo2 { // this is wrong
    	int m;
    	float l;
    } Foo2;*/
    The same reasoning applies here. You can't use static because you aren't creating an instance.

    Thus:

    typedef struct foo2 Foo2;

    Produces:

    static Foo2 myInstance;

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  5. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM

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