forward declaration

This is a discussion on forward declaration within the C Programming forums, part of the General Programming Boards category; Hello, Forward declaration in C I am wondering if this is the correct way to do forward declaration in C. ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    forward declaration

    Hello,

    Forward declaration in C

    I am wondering if this is the correct way to do forward declaration in C.

    I would like to hide the data in my implementation file.

    *.h header file
    Code:
    typedef struct HANDLES;
    
    HANDLES *_handles;
    
    HANDES* handles()
    {
    	return _handles;
    }
    *.c implemenation
    Code:
    typedef struct HANDLES
    {
    	int hDialer;
    	int hDevice;
    };
    in my main program which includes *.h
    Code:
    //setting the hDevice handle
    handles().hDevice = 101012;
    Is this the correct way to do this.

    Many thanks,

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Not quite.

    1) The function handles() still needs to go in a source file, just like any other function, unless you use the inline keyword from C99.

    2) handles() as declared returns a pointer. You use ->, not . on a pointer.

    3)If you hide the data, you cannot access it in main. You cannot access hDevice. Only your implementation file and the functions declared in it can be used to access the data.

    4)
    Code:
    HANDLES *_handles;
    You don't declare variables in a header file without the key words static or extern. static is almost never used in this case. extern is use to reference a global variable, but that variable must be declared in another file without the extern key word or with an initializer.

    Also if you put that pointer in your header file, there is little point for the handles() function.

    5)
    Code:
    typedef struct HANDLES
    {
    	int hDialer;
    	int hDevice;
    };
    This is not how you declare a structure. typedef is used to define an identifier that is to be used as a synonym for a type. It is used exactly like a variable declaration, except the typedef key word comes first. You do not do that here. Here you have a type called "struct HANDLES", who's members you declare, but no identifier to typedef.
    Normally something like this is done:
    Code:
    typedef struct
    {
    	int hDialer;
    	int hDevice;
    } HANDLES;
    Here there is a nameless struct, who's members are spelled out, and an identifier by which it can be referred to.

    6)
    Code:
    typedef struct HANDLES;
    Same as #4. The correct syntax for forward declaring the type struct HANDLES, is
    Code:
    struct HANDLES;
    You cannot forward declare a nameless struct. You can however, typedef a forward declared struct to a different name, so that it is more convenient to use.

    7) Why hide data anyway? It's not a common trick in C. Use an object oriented language like C++ if you want good encapsulation.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by King Mir View Post

    7) Why hide data anyway? It's not a common trick in C.
    Rather common. Most SDKs I have worked - provide only configuration structs in full to the user

    all internal structs are available through handles so user cannot access members directly - only using API
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    I solved the problem like this. If anyone else has a better method. Please post.

    I am really looking for a way that I can share this structure across some other modules. I have a resources.c and a devices.c. I am wondering if I could access the structure from these .c files.

    Thanks,

    Code:
    // handles.h
    struct HANDLES;
    Code:
    //handle.c
    #include "handles.h"
    
    typedef struct
    {
      int hLibrary;
      int hAdapter;
    } HANDLES;
    
    HANDLES *_handles;
    
    HANDLES* handles()
    {
      return _handles;
    }
    
    void get_handle()
    {
      //This will get the handle
      int handle = handles()->hLibrary;
      printf("\nHandle is : %d\n", handle);
    }
    
    void set_handle()
    {
      //Set the handle
      handles()->hLibrary = 100;
    }
    
    int main(void)
    {
      _handles  = (HANDLES*)  malloc(sizeof *_handles);
      set_handle();
      get_handle();
    
    
      return 0;
    }
    Last edited by steve1_rm; 01-29-2009 at 12:32 PM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    your h-file contains nothing, because there is no struct HANDLES anywhere in your code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    If you put everything in the transaction unit with your main program you aren't doing data hiding at all.

    Also if you were to implement data hiding, then you won't be able to do anything like "sizeof *_handles".
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21