Accessing #define in .h files???? 
Author Message
 Accessing #define in .h files????

In a number of .h files that are included in my code there are various
defines (e.g.)

#define MAX 80
#define MIN  1
#define LEN 20

struct foo
{
   int bar;
   char bar2;

Quote:
};

in main or a function how do I access the defines in the .h files

#include "foo.h"
int main()
{
  struct foo bar;
  int temp;
        bar.bar = 20;   /* works fine*/

        temp = bar.MAX     /* does not*/

return 0;       /* note no brackets if your one of those hot on
        /* that subject    */

Quote:
}

How can I access the defines or cant I, or even worse, should defines
like that not be used in .h files????


Sun, 26 Sep 1999 03:00:00 GMT  
 Accessing #define in .h files????

Quote:

> In a number of .h files that are included in my code there are various
> defines (e.g.)

> #define MAX 80
> #define MIN  1
> #define LEN 20

> struct foo
> {
>    int bar;
>    char bar2;
> };

> in main or a function how do I access the defines in the .h files

> #include "foo.h"
> int main()
> {
>   struct foo bar;
>   int temp;
>         bar.bar = 20;   /* works fine*/

>         temp = bar.MAX     /* does not*/
> ...
> }

Wow. Where to begin? I would expect the preprocessor to come up with:

        temp = bar.80

for that last line. Of course this doesn't work. Maybe it's not the
access to the definitions that's the problem; it may be your use of
the definitions that's causing your headaches. Or did you paraphrase
your example off into space somewhere?

You can certainly expect:

        temp = MAX;
        bar.bar = LEN;
        if (temp > MIN)

to work, based on your description, but:

        temp = bar.80

is bizarre.

Maybe,
R.
--
Please remove REMOVE to reply.



Sun, 26 Sep 1999 03:00:00 GMT  
 Accessing #define in .h files????



Quote:
>In a number of .h files that are included in my code there are various
>defines (e.g.)

>#define MAX 80
>#define MIN  1
>#define LEN 20

>struct foo
>{
>   int bar;
>   char bar2;
>};

>in main or a function how do I access the defines in the .h files

>#include "foo.h"
>int main()
>{
>  struct foo bar;
>  int temp;
>        bar.bar = 20;   /* works fine*/

>        temp = bar.MAX     /* does not*/

You've defined MAX as a constant, not a structure member name. The line above
is equivalent to:

         temp = bar.80     /* does not*/

which doesn't make any sense. You could write:

         temp = MAX;

or

         temp = bar.bar;

If that doesn't answer your question you'll need to explain what it is
you want to do.

Quote:
>return 0;       /* note no brackets if your one of those hot on
>        /* that subject    */
>}

>How can I access the defines or cant I, or even worse, should defines
>like that not be used in .h files????

#defines in header files work fine.

--
-----------------------------------------


-----------------------------------------



Sun, 26 Sep 1999 03:00:00 GMT  
 Accessing #define in .h files????

Quote:

>In a number of .h files that are included in my code there are various
>defines (e.g.)
>#define MAX 80
>#define MIN  1
>#define LEN 20
>struct foo
>{
>   int bar;
>   char bar2;
>};
>in main or a function how do I access the defines in the .h files
>#include "foo.h"
>int main()
>{
>  struct foo bar;
>  int temp;
>    bar.bar = 20;   /* works fine*/
>    temp = bar.MAX     /* does not*/

Have you tried "temp = bar.80"? Did it work? See? Maybe we could be
a bit more helpful if we had some information about what "bar.MAX"
is supposed to do in your program.

Kurt

--
| Kurt Watzka                             Phone : +49-89-2180-6254



Sun, 26 Sep 1999 03:00:00 GMT  
 Accessing #define in .h files????


Quote:

>In a number of .h files that are included in my code there are various
>defines (e.g.)

>#define MAX 80
>#define MIN  1
>#define LEN 20

>struct foo
>{
>   int bar;
>   char bar2;
>};

>in main or a function how do I access the defines in the .h files

>#include "foo.h"
>int main()
>{
>  struct foo bar;
>  int temp;
>    bar.bar = 20;   /* works fine*/

>    temp = bar.MAX     /* does not*/

>return 0;   /* note no brackets if your one of those hot on
>    /* that subject    */
>}

>How can I access the defines or cant I, or even worse, should defines
>like that not be used in .h files????

gotch..... it's true.... it should never work! :))
c.... 'bar.bar = 20' works fine, 'cause u r assigning the value 20 to
the field 'bar' of the structure 'bar'!! right??

now, when u do: 'temp = bar.MAX;' the compiler generates:

        temp=bar.80;

which have no meaning..... :?

so, WHY defines??? 'cause u may whish 2 do something like:

        if(bar.bar>MAX)
                printf("Error: ....");

c? if u try to access a field of a struct, that field must exist!!! ;)

hope it helps
jmn



Tue, 28 Sep 1999 03:00:00 GMT  
 Accessing #define in .h files????

Use this:

/* foo.h */
#define  BARMAX  80
#define  BARMIN     1
#define  BARLEN   20

struct foo  {
   int bar;
   char bar2;
 };

/* main.h */
#include "foo.h"
int main()
 {
  struct foo bar;
  int temp;

  bar.bar = 20;

  temp = BARMAX ;  

  return 0;     /* note no brackets if your one of those hot on
        /* that subject    */
 }

In you old program MAX is not an element of structure foo and you can't use
bar.MAX ...

Nick



Wed, 06 Oct 1999 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. access static variable defined in other code file

2. #define - cannot define/undefine preprocessor symbols after first token in file

3. #define defining other #defines

4. Access variable defined in CView class from CMainFrame

5. Crecordset: parameterized access to pre-defined queries

6. Can I access an access (mdb) file within DOS

7. Accessing an MS-Access database file using Visual C++ 4.0

8. Converting Access 97 MDB file to Access 2000

9. accessing outlook from vc (via access mdb file)

10. Help on FILE pointers and file access

11. My exe file needs to access files at an FTP site

12. Access from C progarm file to Perl file

 

 
Powered by phpBB® Forum Software