error C2040: levels of indirection problem.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DrSchwartz
    New Member
    • Feb 2009
    • 10

    error C2040: levels of indirection problem.

    Hi all,
    I defined a struct in the header like this:

    Code:
    #define DFI_HASH_TABLE_SIZE_LOG 16
    #define DFI_HASH_TABLE_SIZE (1<<DFI_HASH_TABLE_SIZE_LOG)
    ...
    typedef struct _s_ip_dfi_hashtable_entry
    {
    	struct _s_ip_dfi_tuples tuples;
    	int action;
    	int rate;
    	int threshold_action;
    	int penalty_rate;
    	int packets;
    }ip_dfi_hashtable_entry;
    Then I tried to initialize in .c file:
    Code:
    ip_dfi_hashtable_entry * dfi_hashtable_ptr;
    dfi_hashtable_ptr = (struct _s_ip_dfi_hashtable_entry *) mem_alloc (sizeof(struct _s_ip_dfi_hashtable_entry)*DFI_HASH_TABLE_SIZE);
    And for the second line from .c file, Igot errors:
    error C2040: 'dfi_hashtable_ ptr' : 'int ' differs in levels of indirection from 'struct _s_ip_dfi_hasht able_entry *'
    error C2099: initializer is not a constant.
    What's wrong with my code?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by DrSchwartz
    And for the second line from .c file, Igot errors:
    error C2040: 'dfi_hashtable_ ptr' : 'int ' differs in levels of indirection from 'struct _s_ip_dfi_hasht able_entry *'
    error C2099: initializer is not a constant.
    What's wrong with my code?
    That's your compiler's cryptic way of telling you that it thinks that mem_alloc() returns an int and you want to cast it to pointer to a structure. Include the corresponding header file(s) for the mem_alloc() function.

    kind regards,

    Jos

    Comment

    • DrSchwartz
      New Member
      • Feb 2009
      • 10

      #3
      Originally posted by JosAH
      That's your compiler's cryptic way of telling you that it thinks that mem_alloc() returns an int and you want to cast it to pointer to a structure. Include the corresponding header file(s) for the mem_alloc() function.

      kind regards,

      Jos
      Thanks for reply. But the header of mem_alloc() function is already included. Can there be another problem?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by DrSchwartz
        Thanks for reply. But the header of mem_alloc() function is already included. Can there be another problem?
        Don't ignore that (cryptic) error diagnostic message: the compiler thinks that you want to use an int as a struct pointer. Show us the declaration of that mem_alloc() function or better yet, include your own declaration just above that offending line:

        Code:
        extern void* mem_alloc(int);
        ... and see what happens.

        kind regards,

        Jos

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          Originally posted by JosAH
          That's your compiler's cryptic way of telling you that it thinks that mem_alloc() returns an int and you want to cast it to pointer to a structure.
          It's already cast to (struct _s_ip_dfi_hasht able_entry *). It would be warning, not error, if were any warning at all. I tried it with int mem_alloc(int) and it compiled on gcc.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Why are you using the nonstandard mem_alloc function?
            Why not use the standard malloc function?
            Are you trying to debug some prior memory allocation problem?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by newb16
              It's already cast to (struct _s_ip_dfi_hasht able_entry *). It would be warning, not error, if were any warning at all. I tried it with int mem_alloc(int) and it compiled on gcc.
              The OP didn't mention which compiler did this nor were the compiler flags mentioned. That error didn't come from nowhere and we can't see it because of the very narrow context given. Nor do we know if C or C++ is the language being used ...

              kind regards,

              Jos

              Comment

              • DrSchwartz
                New Member
                • Feb 2009
                • 10

                #8
                Originally posted by JosAH
                The OP didn't mention which compiler did this nor were the compiler flags mentioned. That error didn't come from nowhere and we can't see it because of the very narrow context given. Nor do we know if C or C++ is the language being used ...

                kind regards,

                Jos
                I'm sorry for a narrow context provided. C is used in my program, and VC6 used as a compiler. I did not specify any extra flags for compiler.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  Originally posted by DrSchwartz
                  error C2099: initializer is not a constant.
                  Is the C2099 error for the same line as the C2040 error?
                  That's interesting because I don't see an initializer.
                  Are you sure there's a semicolon at the end of the first line (the variable definition) to make sure the "=" is not part of the definition?

                  Is there any chance int is 16 bits wide on your machine? If so, DFI_HASH_TABLE_ SIZE might be negative. Consider this instead:
                  Code:
                  #define DFI_HASH_TABLE_SIZE (1uL << DFI_HASH_TABLE_SIZE_LOG)

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by DrSchwartz
                    I'm sorry for a narrow context provided. C is used in my program, and VC6 used as a compiler. I did not specify any extra flags for compiler.
                    Did you try my suggestion in reply #4? You can't just sit and wait here until someone comes up with a solution to your problem. You have to help us help you.

                    kind regards,

                    Jos

                    Comment

                    • DrSchwartz
                      New Member
                      • Feb 2009
                      • 10

                      #11
                      Thank you for your kind help. Actually fall asleep at my desk so I couldn't reply you immediately. Reply #4 didn't help me, so:
                      Instead of weird:
                      Code:
                      dfi_hashtable_ptr = (struct _s_ip_dfi_hashtable_entry *) mem_alloc (sizeof(struct _s_ip_dfi_hashtable_entry)*DFI_HASH_TABLE_SIZE);
                      I just used:
                      Code:
                      ip_dfi_hashtable_entry * dfi_hashtable_ptr[DFI_HASH_TABLE_SIZE];
                      and that worked :)

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by DrSchwartz
                        Thank you for your kind help. Actually fall asleep at my desk so I couldn't reply you immediately. Reply #4 didn't help me, so:
                        Instead of weird:
                        Code:
                        dfi_hashtable_ptr = (struct _s_ip_dfi_hashtable_entry *) mem_alloc (sizeof(struct _s_ip_dfi_hashtable_entry)*DFI_HASH_TABLE_SIZE);
                        I just used:
                        Code:
                        ip_dfi_hashtable_entry * dfi_hashtable_ptr[DFI_HASH_TABLE_SIZE];
                        and that worked :)
                        You must've changed more than that because changing that second line like you showed us implies a redefinition of that pointer. Please be more clear next time when you want to ask a question because this attempt was an obscure mess.

                        kind regards,

                        Jos

                        Comment

                        Working...