extern struct variable

Discussion in 'C++' started by Bangalore, Aug 2, 2005.

  1. Bangalore

    Bangalore Guest

    Hi,
    Like fundamental data types (int,char,float,...),why can't we are
    not able to make userdefined datatypes as extern.

    if "file1.C" conatains
    struct a
    {
    int x;
    } b = { 3 };

    and in "file2.C" if I want to access b.x, by making
    extern struct a b;
    the compiler will flash error.

    Thanks
    Bangalore. :lol:
    Bangalore, Aug 2, 2005
    #1
    1. Advertising

  2. Bangalore

    Srini Guest

    > Hi,
    > Like fundamental data types (int,char,float,...),why can't we are
    > not able to make userdefined datatypes as extern.
    >
    > if "file1.C" conatains
    > struct a
    > {
    > int x;
    > } b = { 3 };
    >
    > and in "file2.C" if I want to access b.x, by making
    > extern struct a b;
    > the compiler will flash error.


    The problem here is that in file2.c, the compiler does not know what
    "struct a" is. For this, it must be able to see its definition. Try
    this...

    // file.h
    struct A
    {
    int x;
    };

    // file1.cpp
    #include "file.h"

    A obj = {3}; // struct keyword not required here! its C++

    // file2.cpp
    #include "file.h"

    extern A obj; // defined elsewhere

    Now you can compile file1.cpp and file2.cpp seperately and link them
    together.

    HTH
    Regards,
    Srini
    Srini, Aug 2, 2005
    #2
    1. Advertising

  3. Bangalore

    Rolf Magnus Guest

    Bangalore wrote:

    > Hi,
    > Like fundamental data types (int,char,float,...),why can't we are
    > not able to make userdefined datatypes as extern.
    >
    > if "file1.C" conatains
    > struct a
    > {
    > int x;
    > } b = { 3 };
    >
    > and in "file2.C" if I want to access b.x, by making
    > extern struct a b;
    > the compiler will flash error.


    That's because the compiler only know that there is a struct called 'a'. It
    doesn't know anything else about it, like that it has a member called 'x'.
    So the compiler cannot use it without seeing the whole definition of the
    struct.
    Rolf Magnus, Aug 2, 2005
    #3
  4. Bangalore

    maadhuu Guest

    hello, what you could do is declare a pointer of type struct and then use
    that.
    maadhuu, Aug 2, 2005
    #4
  5. Bangalore

    Howard Guest

    "maadhuu" <> wrote in message
    news:...
    > hello, what you could do is declare a pointer of type struct and then use
    > that.
    >


    Eh? What's a "pointer of type struct"? And how would a pointer solve the
    problem that the compiler can't see his struct definition (since the struct
    definition is in a file which is not #include'd)?

    -Howard
    Howard, Aug 2, 2005
    #5
  6. Bangalore

    Srini Guest

    > Hi,
    > Like fundamental data types (int,char,float,...),why can't we are
    > not able to make userdefined datatypes as extern.
    >
    > if "file1.C" conatains
    > struct a
    > {
    > int x;
    > } b = { 3 };
    >
    > and in "file2.C" if I want to access b.x, by making
    > extern struct a b;
    > the compiler will flash error.
    >

    The problem here is that in file2.c, the compiler does not know what
    "struct a" is. For this, it must be able to see its definition. Try
    this...

    // file.h
    struct A
    {
    int x;
    };

    // file1.cpp
    #include "file.h"

    A obj = {3}; // struct keyword not required here! its C++

    // file2.cpp
    #include "file.h"

    extern A obj; // defined elsewhere

    Now you can compile file1.cpp and file2.cpp seperately and link them
    together.

    HTH
    Regards,
    Srini
    Srini, Sep 6, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Matthews
    Replies:
    5
    Views:
    2,408
    tom_usenet
    Aug 2, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,372
    Chris Fogelklou
    Apr 20, 2004
  3. Rick Anderson

    "extern struct foobar" linux compilation warning

    Rick Anderson, Mar 9, 2005, in forum: C Programming
    Replies:
    10
    Views:
    633
    Chris Torek
    Mar 9, 2005
  4. Replies:
    18
    Views:
    7,079
    Karl Heinz Buchegger
    Jul 22, 2005
  5. Andre
    Replies:
    5
    Views:
    535
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page