Structure Compatibility

  • Thread starter Vijay Kumar R Zanvar
  • Start date
V

Vijay Kumar R Zanvar

I have the following question:

1. According to 6.2.7P1 of Std, are the
following structures compatible?

(Assume you compile with proper options)

/* compatible.c */
#include <stdio.h>

struct A
{
int a;
char b;
};

int
main ( void )
{
struct A a = { 5, 'a' };
extern struct A b;
b = a;
printf ( "%d %c\n", b.a, b.b );
return 0;
}

/* compatible2.c */
struct A
{
int a;
char b;
};

Thanks
 
M

Mark A. Odell

I have the following question:

1. According to 6.2.7P1 of Std, are the
following structures compatible?

(Assume you compile with proper options)

/* compatible.c */
#include <stdio.h>

struct A
{
int a;
char b;
};

int
main ( void )
{
struct A a = { 5, 'a' };
extern struct A b;

Wouldn't this be a name space colision? I don't understand your use of
"compatible".
 
S

Sheldon Simms

/* compatible.c */
#include <stdio.h>

struct A
{
int a;
char b;
};

int
main ( void )
{
struct A a = { 5, 'a' };
extern struct A b;

This means "declare b as a variable with external linkage of type
struct A". The struct A being referred to here is the struct A at
the beginning of file compatible.c

What you are specifying with the storage class specifier here is
that b is not a local variable in main. You are telling the
compiler to use some other b. Since there is no "other" b
defined in this code, it won't compile.

In the Standard, 6.2.2 says:
4 For an identifier declared with the storage-class specifier
extern ... If no prior declaration is visible ... then the
identifier has external linkage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,077
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top