How to fix errors relating to pointers in C?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shane Fitz
    New Member
    • Nov 2010
    • 4

    How to fix errors relating to pointers in C?

    I am getting a warning and a common error in my code, and I need help understanding them.

    The warning is:[Warning] assignment makes pointer from integer without a cast

    Here is the code that i get the warning from:
    Code:
    line1 = biset_new(MAX_STRING); 
    	line2 = bitset_new(MAX_STRING);
    	bitset_un = bitset_new(MAX_STRING);
    	bitset_in = bitset_new(MAX_STRING);
    the left hand of the equals are all pointers to my bitset structure. The right hand side is a function i have written that returns a pointer to a bitset. MAX_STRING is defined as 256.


    The error Im getting is: dereferencing pointer to incomplete type

    And i get this error when i try to do something like:
    Code:
    bitset_un->size
    Where 'size' is an int varible in the bitset struct. I want to access the size and items in my bitset. How can I do this? My bitset code is included below.

    Code:
    struct bitset{
    	int size; 
    	unsigned items[size];
    };
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    This looks like you are using C, so I will answer in that vein. If you are using C++, there are already templates to handle bitsets.

    The second definition in your bitset structure is not a legal array definition.

    All array definitions must have a size that is computable at compile time.

    Unless you want to play games with structure allocation, the "correct" definition of your bitset structure is probably more like this:
    Code:
    struct bitset{ 
        int size;  
        unsigned *items; 
    };
    And the "constructo r" will be something like this (you should provide appropriate error control, I've simply demonstrated one option using the assert macro):
    Code:
    struct bitset *bitset_new(int size)
    {
      struct bitset *bitset;
      unsigned      *items;
    
      assert(0 <= size);
    
      bitset = (struct bitset *)malloc(sizeof struct bitset);
      items  = (unsigned *)malloc((sizeof unsigned) * size);
    
      assert(NULL != bitset);
      assert(NULL != items);
    
      bitset->items = items;
    
      return bitset;
    }
    Hopefully this helps some.

    Good luck!
    Last edited by Oralloy; Nov 30 '10, 06:59 PM. Reason: Clarified a comment

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Oralloy: only one thing more is needed. Insert this line in the vicinity of your line 14:
      Code:
      bitset->size = size;

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        Thanks Don!

        Comment

        Working...