By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

How to fix errors relating to pointers in C?

P: 4
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:
Expand|Select|Wrap|Line Numbers
  1. line1 = biset_new(MAX_STRING); 
  2.     line2 = bitset_new(MAX_STRING);
  3.     bitset_un = bitset_new(MAX_STRING);
  4.     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:
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. struct bitset{
  2.     int size; 
  3.     unsigned items[size];
  4. };
Nov 30 '10 #1
Share this Question
Share on Google+
3 Replies


Oralloy
Expert 100+
P: 983
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:
Expand|Select|Wrap|Line Numbers
  1. struct bitset{ 
  2.     int size;  
  3.     unsigned *items; 
  4. }; 
  5.  
And the "constructor" will be something like this (you should provide appropriate error control, I've simply demonstrated one option using the assert macro):
Expand|Select|Wrap|Line Numbers
  1. struct bitset *bitset_new(int size)
  2. {
  3.   struct bitset *bitset;
  4.   unsigned      *items;
  5.  
  6.   assert(0 <= size);
  7.  
  8.   bitset = (struct bitset *)malloc(sizeof struct bitset);
  9.   items  = (unsigned *)malloc((sizeof unsigned) * size);
  10.  
  11.   assert(NULL != bitset);
  12.   assert(NULL != items);
  13.  
  14.   bitset->items = items;
  15.  
  16.   return bitset;
  17. }
  18.  
Hopefully this helps some.

Good luck!
Nov 30 '10 #2

Expert 100+
P: 2,398
Oralloy: only one thing more is needed. Insert this line in the vicinity of your line 14:
Expand|Select|Wrap|Line Numbers
  1. bitset->size = size;
Nov 30 '10 #3

Oralloy
Expert 100+
P: 983
Thanks Don!
Nov 30 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.