1

I want to do this:

typedef struct
{
    CGPoint vertices[];
    NSUInteger vertexCount;
} Polygon;

But it says Field has incomplete type CGPoint [].

Geri Borbás
  • 15,810
  • 18
  • 109
  • 172

3 Answers3

4

You need to do one of two things:

  1. Declare the array to be a fixed size (probably not what you want)
  2. Make it a pointer. But then you need to properly malloc and free the array as needed.

A better choice is to not use a struct and instead create a full class. Then you can add methods and properties as well as make memory management much easier. You are working in Objective-C. Take advantage of the Object Oriented aspects of the language. Add a method to calculate the circumference and area, etc. Put the logic where it belongs.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • There's also [a third option](http://stackoverflow.com/questions/7641698/allocating-struct-with-variable-length-array-member). – Jesse Rusak Oct 29 '13 at 18:01
  • @JesseRusak Do you mean use C++? My options were within Objective-C. – rmaddy Oct 29 '13 at 18:03
  • No, I meant to move the array member to the end of the struct and give it a length of 0, and then just allocate more space than needed for the struct. In that case it's neither a pointer nor a fixed size. – Jesse Rusak Oct 29 '13 at 18:06
  • Thanks anyway, but creating objects for this is not lightweight enough. :D Just organizing some code from the past, and I really did not want to introduce anything new, moreover cut down instead. – Geri Borbás Oct 29 '13 at 18:06
  • @Geri Making it a class doesn't really make it "heavier weight". The memory footprint of the class is only going to be a few bytes larger than the `struct`. – rmaddy Oct 29 '13 at 18:08
  • Using class would put those heavy weight on semantics, not meant performance or anything like this. Just wanted to be the less specific as I could. – Geri Borbás Oct 29 '13 at 18:10
  • @Geri It all depends on what you plan to do with the `struct`. If you write any function or method on your `struct` then it might as well be on the `Polygon` class instead. You must have some logic using the `struct`. Put the logic where belongs - in a class. Either way, enjoy. – rmaddy Oct 29 '13 at 18:13
0

Set array size CGPoint vertices[count];

Nekak Kinich
  • 905
  • 7
  • 10
0

Don't you want a unique name for each element of your struct anyway? If you just want a bunch of CGPoint's in a numerical order, with the ability to count how many of them there are you'd be much better served by shoving them in an NSArray or NSMutableArray (stored as NSValue's of course)

The whole point of a struct would be to have easy access to the values by a descriptive name, ie:

typedef struct {
    CGPoint helpfulAndDescriptiveNameOne;
    CGPoint helpfulAndDescriptiveNameTwoWhichIsDifferentThanTheOtherName;
    etc...
    NSUInteger vertexCount;
}

For example, a CGRect is just a struct composed of four different CGFloats, each of which is descriptively and helpfully named:

typedef {
    CGFloat x;
    CGFloat y;
    CGFloat width;
    CGFloat height;
} CGRect;
ryan cumley
  • 1,901
  • 14
  • 11