Jaiesh_bhai
Jaiesh_bhai

Reputation: 1814

How do I access #define Variable from one structure in a C file?

#define MAX 7
#define BUFFER 16
#define MODULO 8

typedef struct {
int x;
} BLAH;

if I have:

  checkWindow(BLAH *b) {  
  int mod;   
  mod = b.MODULO; 
  }

Specifically can I access MODULO from the BLAH structure?

Upvotes: 0

Views: 142

Answers (2)

Punit Soni
Punit Soni

Reputation: 1289

All #defines will be replaced in plain text by the "values" they define, before compilation. They are not variables, just short-hand syntax to make writing programs easy. None of your #def stuff actually reaches the compiler, its resolved in preprocessor.

Now, if you simply replace MODULO in your example by 8, does the resulting code make sense to you? If it does make sense, please take a Computer Programming 101 course.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

I think you misunderstand the meaning of preprocessor definitions. #define-d items only look like variables, but they are not variables in the classical sense of the word: they are text substitutions. They are interpreted by the preprocessor, before the compiler gets to see the text of your program. By the time the preprocessor is done, the text of the program has no references to MAX, BUFFER, or MODULO: their occurrences are substituted with 7, 16, and 8. That is why you cannot access #define-d variables: there are no variables to access.

Upvotes: 2

Related Questions