Isnin, 3 September 2018

BOOK OF STRUCTURE IN C +++

struct tag_name {
   type member1;
   type member2;
   /* declare as many members as desired, but the entire structure size must be known to the compiler. */
};
typedef struct tag_name {
   type member1;
   type member2;
} struct_alias;
ypedef struct tag_name struct_alias;

// These two statements now have the same meaning:
// struct tag_name struct_instance;
// struct_alias struct_instance;
struct account {
   int account_number;
   char *first_name;
   char *last_name;
   float balance;
};
struct account s;
/* Forward declare a type "point" to be a struct. */
typedef struct point point;
/* Declare the struct with integer members x, y */
struct point {
   int    x;
   int    y;
};
/* Define a variable p of type point, and initialize its first two members in place */
point p = { 1, 2 };
/* Define a variable p of type point, and set members using designated  initializers*/
point p = { .y = 2, .x = 1 };
#include <stdio.h>

/* Define a type point to be a struct with integer members x, y */
typedef struct {
   int    x;
   int    y;
} point;

int main(void) {

/* Define a variable p of type point, and initialize all its members inline! */
    point p = { 1, 3 };

/* Define a variable q of type point. Members are uninitialized. */
    point q;

/* Assign the value of p to q, copies the member values from p into q. */
    q = p;

/* Change the member x of q to have the value of 3 */
    q.x = 3;

/* Demonstrate we have a copy and that they are now different. */
    if (p.x != q.x) printf("The members are not equal! %d != %d", p.x, q.x);

/* Define a variable r of type point. Members are uninitialized. */
    point r;

/* Assign values using compound literal (ISO C99/supported by GCC > 2.95) */
    r = (point) { 1, 2 };

    return 0;
}
struct point {
   int x;
   int y;
};
struct point my_point = { 3, 7 };
struct point *p = &my_point;  /* To declare and define p as a pointer of type struct point,
                                 and initialize it with the address of my_point. */

(*p).x = 8;                   /* To access the first member of the struct */
p->x = 8;                     /* Another way to access the first member of the struct */
typedef struct list_element list_element;
struct list_element {
   point p;
   list_element * next;
};
list_element el = { .p = { .x = 3, .y =7 }, };
list_element le = { .p = { .x = 4, .y =5 }, .next = &el };
typedef struct {
   int    account_number;
   char   *first_name;
   char   *last_name;
   float  balance;
} account;
struct point {
   int    x;
   int    y;
};
struct point *(*point_compare_func) (struct point *a, struct point *b);
typedef struct point point_type;
struct point {
   int    x;
   int    y;
};
point_type *(*point_compare_func) (point_type *a, point_type *b);
typedef struct point point;
struct point {
   int    x;
   int    y;
};
point *(*point_compare_func) (point *a, point *b);
/* Using the struct point type from before */

/* Define a function that returns a pointer to the biggest point,
   using a function to do the comparison. */
struct point *
biggest_point (size_t size, struct point *points,
               struct point *(*point_compare) (struct point *a, struct point *b))
{
    int i;
    struct point *biggest = NULL;

    for (i=0; i < size; i++) {
        biggest = point_compare(biggest, points + i);
    }
    return biggest;
}
typedef point *(*point_compare_func_type) (point *a, point *b);
/* Using the struct point type from before and the typedef for the function pointer */

/* Define a function that returns a pointer to the biggest point,
   using a function to do the comparison. */
point *
biggest_point (size_t size, point * points, point_compare_func_type point_compare)
{
    int i;
    point * biggest = NULL;

    for (i=0; i < size; i++) {
        biggest = point_compare(biggest, points + i);
    }
    return biggest;
}
/* Example for namespace clash */

typedef struct account { float balance; } account;
struct account account; /* possible */
account account; /* error */

1 ulasan:

  1. struct tag_name {
    type member1;
    type member2;
    /* declare as many members as desired, but the entire structure size must be known to the compiler. */
    };
    typedef struct tag_name {
    type member1;
    type member2;
    } struct_alias;
    ypedef struct tag_name struct_alias;

    // These two statements now have the same meaning:
    // struct tag_name struct_instance;
    // struct_alias struct_instance;
    struct account {
    int account_number;
    char *first_name;
    char *last_name;
    float balance;
    };
    struct account s;
    /* Forward declare a type "point" to be a struct. */
    typedef struct point point;
    /* Declare the struct with integer members x, y */
    struct point {
    int x;
    int y;
    };
    /* Define a variable p of type point, and initialize its first two members in place */
    point p = { 1, 2 };
    /* Define a variable p of type point, and set members using designated initializers*/
    point p = { .y = 2, .x = 1 };
    #include

    /* Define a type point to be a struct with integer members x, y */
    typedef struct {
    int x;
    int y;
    } point;

    int main(void) {

    /* Define a variable p of type point, and initialize all its members inline! */
    point p = { 1, 3 };

    /* Define a variable q of type point. Members are uninitialized. */
    point q;

    /* Assign the value of p to q, copies the member values from p into q. */
    q = p;

    /* Change the member x of q to have the value of 3 */
    q.x = 3;

    /* Demonstrate we have a copy and that they are now different. */
    if (p.x != q.x) printf("The members are not equal! %d != %d", p.x, q.x);

    /* Define a variable r of type point. Members are uninitialized. */
    point r;

    /* Assign values using compound literal (ISO C99/supported by GCC > 2.95) */
    r = (point) { 1, 2 };

    return 0;
    }
    struct point {
    int x;
    int y;
    };
    struct point my_point = { 3, 7 };
    struct point *p = &my_point; /* To declare and define p as a pointer of type struct point,
    and initialize it with the address of my_point. */

    (*p).x = 8; /* To access the first member of the struct */
    p->x = 8; /* Another way to access the first member of the struct */
    typedef struct list_element list_element;
    struct list_element {
    point p;
    list_element * next;
    };
    list_element el = { .p = { .x = 3, .y =7 }, };
    list_element le = { .p = { .x = 4, .y =5 }, .next = &el };
    typedef struct {
    int account_number;
    char *first_name;
    char *last_name;
    float balance;
    } account;
    struct point {
    int x;
    int y;
    };
    struct point *(*point_compare_func) (struct point *a, struct point *b);
    typedef struct point point_type;
    struct point {
    int x;
    int y;
    };
    point_type *(*point_compare_func) (point_type *a, point_type *b);
    typedef struct point point;
    struct point {
    int x;
    int y;
    };
    point *(*point_compare_func) (point *a, point *b);
    /* Using the struct point type from before */

    /* Define a function that returns a pointer to the biggest point,
    using a function to do the comparison. */
    struct point *
    biggest_point (size_t size, struct point *points,
    struct point *(*point_compare) (struct point *a, struct point *b))
    {
    int i;
    struct point *biggest = NULL;

    for (i=0; i < size; i++) {
    biggest = point_compare(biggest, points + i);
    }
    return biggest;
    }
    typedef point *(*point_compare_func_type) (point *a, point *b);
    /* Using the struct point type from before and the typedef for the function pointer */

    /* Define a function that returns a pointer to the biggest point,
    using a function to do the comparison. */
    point *
    biggest_point (size_t size, point * points, point_compare_func_type point_compare)
    {
    int i;
    point * biggest = NULL;

    for (i=0; i < size; i++) {
    biggest = point_compare(biggest, points + i);
    }
    return biggest;
    }
    /* Example for namespace clash */

    typedef struct account { float balance; } account;
    struct account account; /* possible */
    account account; /* error */

    BalasPadam