Thread: cannot compile with extern struct

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    72

    cannot compile with extern struct

    Hi All

    I'm trying to setup a struct in a file called mytest.c, which will be included/used in my main file

    I have the following test program:

    mytest.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct CONFIG {
      int *x ;
    } *_c ;
    
    void init_extern(void)
    {
      _c = malloc(sizeof(*_c)) ;
      int f = 11 ;    //   or _c->x = malloc( sizeof(*_c->x) ) ;
      _c->x = &f ; //       *_c->x = 13 ;
      printf("value is %d (%p)\n", *_c->x, &_c );
    }
    mytest.h
    Code:
    #ifndef ADD_H
    #define ADD_H
     
    void init_extern(void);
    
    extern struct CONFIG *_c ;
     
    #endif /* ADD_H */
    and the main program is:

    Code:
    #include <stdio.h>
    #include "mytest.h"
    
    int main(void)
    {
      init_extern();
      printf("value is %d (%p)\n", *_c->x, &_c );
      return 0;
    }
    When I try to compile I get

    Code:
    $ gcc mymain.c mytest.c
    mymain.c: In function ‘main’:
    mymain.c:7: error: dereferencing pointer to incomplete type
    The problem is with the printf in main. However in mytest.c it works just fine (if I comment out that line). Any suggestions ?

    thnx
    LuCa

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The solution is to place the struct definition in mytest.h:
    Code:
    #ifndef ADD_H
    #define ADD_H
     
    struct CONFIG {
      int *x ;
    };
    
    void init_extern(void);
    
    extern struct CONFIG *_c;
     
    #endif /* ADD_H */
    Now mytest.c would be:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "mytest.h"
    
    struct CONFIG *_c;
    
    void init_extern(void)
    {
      _c = malloc(sizeof(*_c)) ;
      int f = 11 ;    //   or _c->x = malloc( sizeof(*_c->x) ) ;
      _c->x = &f ; //       *_c->x = 13 ;
      printf("value is %d (%p)\n", *_c->x, &_c );
    }
    EDIT:
    By the way, I presume that this is for "educational purposes"? By setting _c->x = &f in init_extern(), you are asking for trouble since f is a local variable that is destroyed when the function returns to the caller.
    Last edited by laserlight; 06-08-2009 at 02:04 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    72
    thnx, it works (and yes I'm learning C!)

    So

    Code:
    int f = 11 ; 
    _c->x = &f ;
    isn't there in C something like reference counting ? So, even if f goes out of scope, _c->x is still pointing to the memory containing the value ?

    cheers
    LuCa

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Oh, good grief. No, C doesn't have a garbage collector, nor does it keep track of memory like that.

    C is basically assembly with a standard to guarantee portability and much prettier, higher level syntax. There are some things taken care of by the compiler that make it easier to program, but the design of the language assumes that you, the programmer, have a clue about what you're doing. Therefore, it leaves most memory issues up to you, the programmer, to figure out how to allocate and deallocate by yourself.

    Returning the address of a local variable is horrible and likely to result in some very weird bugs.
    Last edited by MacGyver; 06-08-2009 at 02:34 AM.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    72
    ok, thats clear!! thnx for the help!
    Last edited by jeanluca; 06-08-2009 at 03:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM