7
#include <stdio.h>    
typedef  struct
    {       
        int   num ;
    } NUMBER ;

    int main(void)
    {   
        struct NUMBER array[99999];
        return 0;
    }

I'm getting a compile error:

error: array type has incomplete element type

I believe the problem is that I'm declaring the array of struct incorrectly. It seems like that's how you declare it when I looked it up.

2 Answers 2

15
struct NUMBER array[99999];  

should be

NUMBER array[99999];  

because you already typedefed your struct.


EDIT: As OP is claiming that what I suggested him is not working, I compiled this test code and it is working fine:

#include <stdio.h>
typedef  struct
{
    int   num ;
} NUMBER ;

int main(void)
{
    NUMBER array[99999];
    array[0].num = 10;
    printf("%d", array[0].num);
    return 0;
}  

See the running code.

Sign up to request clarification or add additional context in comments.

6 Comments

or struct NUMBER{int num;}; without the typedef
NUMBER array[99999]; gives me more errors. One of them states: NUMBER isn't declared.
No. The only Expected warning/error should be: [Warning] unused variable 'array' [-Wunused-variable]. See the code here: ideone.com/JIjxy8
error: 'NUMBER' undeclared (first use in this function) error: (Each undeclared identifier is reported only once error: for each function it appears in.) error: expected ';' before 'array'
Which compiler are you using?
|
3

You have

typedef  struct
    {       
        int   num ;
    } NUMBER ;

which is a shorthand for

struct anonymous_struct1
    {       
        int   num ;
    };
typedef struct anonymous_struct1 NUMBER ;

You have now two equivalent types:

struct anonymous_struct1
NUMBER

You can use them both, but anonymous_struct1 is in the struct namespace and must always be preceded with struct in order to be used. (That is one major difference between C and C++.)

So either you just do

NUMBER array[99999];

or you define

typedef  struct number
    {       
        int   num ;
    } NUMBER ;

or simply

struct number
    {       
        int   num ;
    };

and then do

struct number array[99999];

4 Comments

struct number { int num ; };
I tried your last method, but it still says: error: array type has incomplete element type
I'm getting this error as well. My struct contains a struct of itself. So I'm wondering if it's not defined yet?
It's because you can't define a sized array that includes a sized array of itself, and that makes sense obviously :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.