Thread: #define within struct definition?

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    27

    #define within struct definition?

    How is this different from a regular #define that's just on it's own? Does containing the #define within the struct definitions means that you can't assign the #define constant to other things besides the struct's members?

    I'm talking about something like this:
    Code:
    typedef struct STRUCT_NAME {
    
    //
    // some struct member fields.
    //
    
    int a;
    
    #define CONSTANT_A 0
    .
    .
    .
    
    //
    // maybe some more members.
    //
    
    };
    As opposed to
    Code:
    #define CONSTANT_A 0
    
    typedel struct STRUCT_NAME {
    .
    .
    .
    };

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    As far as I know there's no difference. It's still a precompiler def.
    I have no proof though. Other than it wouldn't make any sense and I've never heard of it.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    A #define only affects everything in the code that's below it in the file (until you reach the end of the file, or a #undef for it). Without looking at a complete example, it's hard to say whether or not it would act the same if you moved the #define to the top of the file.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You can place a define within a structure declaration. It doesn't have to be special, and you can use it outside of the structure declaration, but it's generally a good idea for the define to be pretty closely related to the structure.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by itsme86
    A #define only affects everything in the code that's below it in the file (until you reach the end of the file, or a #undef for it).
    the #define is not limited to file scope. I's valid to the end of the compilation unit ( or up to an #undef ).
    Kurt

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by ZuK
    the #define is not limited to file scope. I's valid to the end of the compilation unit ( or up to an #undef ).
    Kurt
    BZZZZZZT!!! Try again. The #define must be part of the file in which you use it (or at least inserted into the file using #include), and it must be be before you use it in that file:
    Code:
    itsme@itsme:~/C$ cat deftest1.c
    #include <stdio.h>
    
    int main(void)
    {
      printf("%d\n", MYDEF);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ cat deftest2.c
    #define MYDEF 86
    itsme@itsme:~/C$ gcc -Wall deftest1.c deftest2.c -o deftest
    deftest1.c: In function `main':
    deftest1.c:5: error: `MYDEF' undeclared (first use in this function)
    deftest1.c:5: error: (Each undeclared identifier is reported only once
    deftest1.c:5: error: for each function it appears in.)
    itsme@itsme:~/C$
    Last edited by itsme86; 06-23-2006 at 03:30 PM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    deftest.c is a different compilation unit
    Kurt

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    But those are separate compilation units, so ZuK's statement stands.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well I'm not sure what you mean by "compilation unit", unless you mean file scope plus #included files, which is common sense to me and was implied by statement.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    gcc -Wall deftest1.c deftest2.c -o deftest
    This example acually calls the compiler twice. once for deftest1.c and once for deftest2.c --> 2 compilation units.
    Kurt

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Okay then, "compilation unit" was what I originally meant by "file".
    If you understand what you're doing, you're not learning anything.

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The term used by the C standard is "translation unit".
    C89 Draft

    A C program need not all be translated at the same time. The text of the program is kept in units called source files in this Standard. A source file together with all the headers and source files included via the preprocessing directive #include , less any source lines skipped by any of the conditional inclusion preprocessing directives, is called a translation unit. Previously translated translation units may be preserved individually or in libraries. The separate translation units of a program communicate by (for example) calls to functions whose identifiers have external linkage, by manipulation of objects whose identifiers have external linkage, and by manipulation of data files. Translation units may be separately translated and then later linked to produce an executable program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing syscalls from C
    By lilcoder in forum C Programming
    Replies: 17
    Last Post: 09-19-2007, 02:27 PM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 12:52 PM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM