2

my test code is below:

main1.c:

#include <stdio.h>
extern struct tt ;
int main()
{
    struct tt y;
    y.a=255;
    y.b=0;
    printf("tt->a=%#x   ,tt->b=%#x \n",y.a,y.b);
}

main2.c:

#include<stdio.h>

 struct tt
{
    int a;
    int b;
};

makefile:

main: main1.o
    gcc -o main main1.o
main1.o:  main2.c main1.c

but the compiler reports:

cc    -c -o main1.o main1.c
main1.c:2: warning: useless storage class specifier in empty declaration
main1.c: In function ‘main’:
main1.c:5: error: storage size of ‘y’ isn’t known
make: *** [main1.o] Error 1

how do I write code to in a .c file use the struct defined in another .c file ???

thx for your help!

1
  • 2
    struct should be defined in .h file Commented Mar 27, 2012 at 2:06

2 Answers 2

5

You need to define the struct in a header file that both .c files include. For example:

#ifndef __INCLUDE_GUARD_HERE__
#define __INCLUDE_GUARD_HERE__

struct tt {
  int a;
  int b;
};

#endif

Now both .c files can #include the header file.

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

1 Comment

Is there a way to not to have the definition inside the header file? Let's say the design enforces me to define the struct inside a .c file? Could I access this struct from another .c file somehow? See: stackoverflow.com/questions/23520877/…
2

the error message is very clear. ( error: storage size of 'y' isn't known)

you declare a variable as the name 'yy'

struct tt yy;

but you use another variable with the name 'y' (it doesn't even exist in this scope)

y.a=255;

see? yy != y

1 Comment

It is my mistake, but it is not the problem.

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.