Results 1 to 6 of 6

Thread: Can we use #define inside the structure?

  1. #1
    Join Date
    Dec 2009
    Posts
    23

    Can we use #define inside the structure?

    I am at the beginner stage in the programming world. I have been learning programming language like C, C++, Core Java, SQL, PHP and HTML. I just love to do programming stuff. I do my programming on DELL Studio One 19 desktop computer. Recently i tried one program (i.e given below)
    struct people
    {
    #define ram 0
    #define tom 1
    #define neel 2

    int n;
    };

    int main()
    {
    people temp;
    temp.n= people::tom;
    return 0;
    }
    When trying this code, i am getting the compiler error. Please someone help me to fix this problem.
    Last edited by Harper 21; 18-01-2010 at 08:57 AM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Can we use #define inside the structure?

    My personal recommendation and advice would be not to use the #define in the structures in your program. It will give you the error. Instead of that use the const keyboard, it would give you perfect output. #define keyword, in most part of the C programming is hangover.
    Example:
    struct person
    {
    const int ram = 0;
    const int tom = 1;
    const int neel = 2;

    int n;
    };
    I think this method will work for you and would be able to fix your problem.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Can we use #define inside the structure?

    Actually the method you are trying is the wrong way to do it. And i would tell that the #define's are not syntactically part of the struct(i.e structure). The #define's are always preprocessed out at the time before the compiler parses the struct(structure) definitions. As you are the beginner programmer, don't try out doing new things, just do the simple programming and get the result. Hope your problem will be fix.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Can we use #define inside the structure?

    According the #define function or keyword you are using it not the correct way to use it. The #define keyword is mostly use for textual replacement. When you write something like this #define ram 0, means actually you are saying to the compiler (preprocessor). In short, your code says that you would like to change all occurrences of the text 'ram' in my file to the text '0'. Until you have the text 'ram' someplace in your code, it is a meaningless operation lost before the compilation stage. So, avoid using the #define keyword.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Can we use #define inside the structure?

    My knowledge says that the Struct(structures) is the convenient/easier way to bundle/pack a number of attributes together, using access methods, and not needed to write an explicit class. The Struct class is a generator of specific classes, each one of which is defined to hold a set of variables and their accessors. In struct(i.e structures) never use the #define keyword or method, it will not be able to run that program successfully.

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Can we use #define inside the structure?

    I would every programmer in t he world that you should always try to write down the efficient/simpler code. And on the other hand, if you are inexperienced/beginner programmer and you will try to optimize by worrying about bytes versus int, then you are wasting the time. Write and develop good clean code. So, avoid trying new concept or idea into your program until you become very expert programmer.

Similar Threads

  1. How to Define Namespaces in PHP?
    By SKREECH in forum Software Development
    Replies: 5
    Last Post: 21-02-2010, 02:00 AM
  2. How to define Union?
    By seema_thk in forum Software Development
    Replies: 5
    Last Post: 15-12-2009, 02:07 PM
  3. Define the term- CLR for .NET
    By Jesus2 in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 09:10 PM
  4. Equivalent of #define in C#
    By Luis234 in forum Software Development
    Replies: 6
    Last Post: 20-04-2009, 11:56 PM
  5. How to define an IP address on Linux
    By Markos in forum Tips & Tweaks
    Replies: 0
    Last Post: 11-12-2008, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,504,689.68163 seconds with 17 queries