Adri
Adri

Reputation: 243

How to define a struct correctly

I have a struct where I define the size (width, height) of a square, and I don't know why the code doesn't work well. Here's the code that I'm using:

.h
  struct size{
        int width;
        int height;
   };

.m

   struct size a;
    a.width = 508;
    a.height = 686;
// I use it here.

Any ideas?

Upvotes: 1

Views: 185

Answers (1)

DarkDust
DarkDust

Reputation: 92306

If you want to use Apple provided types, you have:

  • CGSize for sizes (with width and height)
  • CGPoint for locations (with x and y)
  • and CGRect, which combines the two.

Example usage:

CGPoint p;
CGSize s;
CGRect r;

p.x = 1;
p.y = 2;
// or:
p = CGPointMake(1, 2);

s.width = 3;
s.height = 4;
// or:
s = CGSizeMake(3, 4);

r.origin.x = 1;
r.origin.y = 2;
r.size.width = 3;
r.size.height = 4;
// or:
r.origin = p;
r.size = s;
// or:
r = CGRectMake(1, 2, 3, 4);

Upvotes: 5

Related Questions