structs:
Simple Example


Two ways to declare and use structs in C: In C there are several standard ways to make use of structs:

  1. The program on the left below uses the "traditional" method for C structs. The "struct" declaration doesn't allocate storage, but defines point to be a struct. However, in subsequent references to the struct point, you have to include a "hint" and write struct point instead of just point. Leaving off any of these "hints" is an error in C.
  2. The program in the middle uses the "typedef" method. Again, with the typedef struct point no storage is allocated, but now the "hint" must not be present. Putting in any of there "hints" is an error in C.
  3. Usually in this course I will be using the form on the left, without the "typedef" and with the explicit "hints".
  4. The program at the right illustrates C++. Here just a simple struct declaration behaves like a typedef, so later "hints" are not expected. For compatibility the are allowed, however. (C++ didn't like the abs function, so I threw it in as a macro.)
  5. There are other ways to declare structs in C, not even counting the use of malloc.
  6. The more normal way to create structs in C is to use malloc, as we will see with many examples.

"Traditional" structs in C "typedef" structs in C structs in C++
#include <stdio.h>

struct point {/* NO STORAGE ALLOC */
   int x;
   int y;
};

struct rect {/* NO STORAGE ALLOC */
   struct point pt1;
   struct point pt2;
};

int width(struct rect );
int height(struct rect );

int main() {
   struct rect box;
   box.pt1.x = 3;
   box.pt1.y = 2;
   box.pt2.x = 11;
   box.pt2.y = 7;
   printf("width: %i, height: %i\n",
      width(box), height(box));
}

int width(struct rect r) {
   return abs(r.pt1.x - r.pt2.x);
}

int height(struct rect r) {
   return abs(r.pt1.y - r.pt2.y);
}
#include <stdio.h>

typedef struct {/* NO STORAGE ALLOC */
   int x;
   int y;
} point;

typedef struct {/* NO STORAGE ALLOC */
   point pt1;
   point pt2;
} rect;

int width(rect );
int height(rect );

int main() {
   rect box;
   box.pt1.x = 3;
   box.pt1.y = 2;
   box.pt2.x = 11;
   box.pt2.y = 7;
   printf("width: %i, height: %i\n",
      width(box), height(box));
}

int width(rect r) {
   return abs(r.pt1.x - r.pt2.x);
}

int height(rect r) {
   return abs(r.pt1.y - r.pt2.y);
}
#include <stdio.h>
#define abs(x) (x > 0 ? (x) : -(x))
struct point {/* NO STORAGE ALLOC */
   int x;
   int y;
};

struct rect {/* NO STORAGE ALLOC */
   point pt1;/* "struct" optional */
   point pt2;/* "struct" optional */
};

int width(rect );/* "struct" opt */
int height(rect );/* "struct" opt */

int main() {
   rect box;/* "struct" optional */
   box.pt1.x = 3;
   box.pt1.y = 2;
   box.pt2.x = 11;
   box.pt2.y = 7;
   printf("width: %i, height: %i\n",
      width(box), height(box));
}

int width(rect r) {/* "struct" opt */
   return abs(r.pt1.x - r.pt2.x);
}

int height(rect r) {/* "struct" opt */
   return abs(r.pt1.y - r.pt2.y);
}
Common Output C++ Output
% cc -o boxes boxes.c
% boxes
width: 8, height: 5
% CC -o boxes3 boxes3.c
% boxes3
width: 8, height: 5


Extensions of this example:

Using extra function Using pointers to structs
#include <stdio.h>

struct point {
   int x;
   int y;
};

struct rect {
   struct point pt1;
   struct point pt2;
};

int width(struct rect );
int height(struct rect );
struct point makepoint(int , int );

int main() {
   struct rect box;
   box.pt1 = makepoint(3, 2);
   box.pt2 = makepoint(11, 7);
   printf("width: %i, height: %i\n",
      width(box), height(box));
}

int width(struct rect r) {
   return abs(r.pt1.x - r.pt2.x);
}

int height(struct rect r) {
   return abs(r.pt1.y - r.pt2.y);
}

struct point makepoint(int x, int y) {
   struct point temp;
   
   
   temp.x = x;
   temp.y = y;
   return temp;
}
#include <stdio.h>

struct point {
   int x;
   int y;
};

struct rect {
   struct point pt1;
   struct point pt2;
};

int width(struct rect );
int height(struct rect );
struct point *makepoint(int , int );

int main() {
   struct rect box;
   box.pt1 = *makepoint(3, 2);
   box.pt2 = *makepoint(11, 7);
   printf("width: %i, height: %i\n",
      width(box), height(box));
}

int width(struct rect r) {
   return abs(r.pt1.x - r.pt2.x);
}

int height(struct rect r) {
   return abs(r.pt1.y - r.pt2.y);
}

struct point *makepoint(int x, int y) {
   struct point *temp =
      (struct point *)
       malloc(sizeof(struct point));
   (*temp).x = x; /* temp -> x = x */
   (*temp).y = y; /* temp -> y = y */
   return temp;
}
Common Output
% cc -o Boxes Boxes.c
% Boxes
width: 8, height: 5


Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.