is this correct

Discussion in 'C Programming' started by Darklight, Jun 30, 2004.

  1. Darklight

    Darklight Guest

    What is written below i will use as a quick reference i just want to make
    sure i have got it right
    /* STRUCTURES */

    TO DEFINE A STRUCTURE
    way 1. To declare a structure and instance/s together.
    struct coord{ /* struct keyword is used to declare a stucture, coord
    is the structure name or tag */
    int x;
    int y; /* x & y are the names of the member variables of the
    structure */
    } first, second; /* first & second are the instances of the structure */
    /* each instance contains two integers */

    WAY 2. TO DECLARE INSTANCE IN A DIFFERENT LOCATION
    struct coord{
    int x;
    int y;
    };
    /* additional code goes here */
    struct coord first, second;

    ACCESSING STRUCTURE MEMBERS.
    first.x = 50;
    first.y = 100; /* to access a structure member you have to use the
    dot (.) operator between */
    /* the structure name and the member name */

    WAY 3. TO DEDCLARE AND INITIALIZE A STRUCTURE.
    struct time{
    int hours;
    int minutes;
    int seconds;
    }time_of_birth = {8,45,0};

    TO COPY INFORMATION BETWEEN STRUCTURES.
    first = second;
    first = second;

    the above is equivalent to
    first.x = second.x;
    first.y = second.y;

    STRUCTURES THAT CONTAIN ARRAYS
    struct data{
    int x[4];
    char y[10]
    }record;

    record.y /* IS A POINTER TO THE FIRST ELEMENT OF THE ARRAY */

    ARRAYS OF STRUCTURES
    struct entry{
    char fname[10];
    char lname[12];
    char phone[8];
    };
    struct entry list[100]; /* declaring an array of structures of type entry */
    /* each element is a structure of type entry */
    /* each element of list array has three elements */
    /* IE: first element list[0] = list[0].fname,
    list[0].lname, list[0].phone */
    /* last element list[999] = list[999].fname,
    list[999].lname, list[999].phoone */

    INITIALIZING STRUCTURES

    struct customer{
    char firm[20];
    char contact[25];
    };

    struct sale{
    struct customer buyer; /* structure to a structure */
    char item[20];
    float amount;
    };

    struct sale y1990[100] = { /* initializing the structures */
    {{ "acme industries", "George Adams"},
    "left-hand widget",
    1000.00
    },
    {{ "Wilson & co.","Ed Wilson"},
    "Type 12 gizmo",
    200.00
    }
    };

    TO DISPLAY THE ABOVE STRUCTURES

    for(i = 0; i < 2; i++)
    {
    printf("%s %s\n %s £%.2f\n\n", y1990.buyer.firm,
    y1990.buyer.contact, y1990.item, y1990.amount);
    }

    POINTERS AS STRUCTURE MEMBERS
    struct data{
    int *value;
    int *rate
    }first; /* You must initialize pointers to point at some
    thing */
    /* to do this you do as below */

    first.value = &cost;
    first.rate = &interest; /* cost and interest must have been declared as int variables */
    /* When using char variable do not use the &
    operator */

    POINTERS TO STRUCTURES
    struct part{ /* define a structure */
    int number;
    char name[10];
    };
    struct part *p_part; /* declare a pointer to type part;
    struct part gizmo; /* declare an instance to type part */
    p_part = &gizmo; /* perform pointer initialization */

    (*p_part).number = 100; /* this assigns the value 100 to
    gizmo.number*/
    /* this uses the indirection operator (*)*/
    p_part->number /* this is used to access structure members
    using a pointer */
    /* this uses the indirect membership operator
    (->) */

    THREE WAYS TO ACCESS A STUCTURE MEMBER
    str.memb;
    (*p_str).memb;
    p_str->memb;

    POINTERS AND ARRAYS OF STRUCTURES
    struct part{ /* define struture */
    int number;
    char name[10];
    };

    struct part data[100]; /* declare an array of type part */
    struct part *p_part; /* declare a pointer to type part */
    p_part = &data[0]; /* initialize pointer to point to first element in array */
    p_part = data; /* second way to do above */

    printf("%s %s", p_part->number, p_part->name);
    /* to print contents of first element */

    PASSING STRUCTURES AS ARGUMENTS TO FUNCTIONS
    struct data{ /* define and initialize a structure */
    float amount;
    char fname[30];
    char lname[30];
    }rec;

    void print_rec(struct data x); /* declare function prototype*/
    /* addditional statements go here */
    print_rec(rec); /* call function */

    void print_rec(struct data x) /* declare or define function definition */
    {
    printf("\nDoners name %s %s gave £%.2f\n",x.fname,
    x.lname,x.amount);
    }
     
    Darklight, Jun 30, 2004
    #1
    1. Advertising

  2. Darklight

    Richard Bos Guest

    Darklight <> wrote:

    > What is written below i will use as a quick reference i just want to make
    > sure i have got it right
    > /* STRUCTURES */
    >
    > TO DEFINE A STRUCTURE
    > way 1. To declare a structure and instance/s together.
    > struct coord{ /* struct keyword is used to declare a stucture, coord
    > is the structure name or tag */
    > int x;
    > int y; /* x & y are the names of the member variables of the
    > structure */
    > } first, second; /* first & second are the instances of the structure */
    > /* each instance contains two integers */


    Yes.

    > WAY 2. TO DECLARE INSTANCE IN A DIFFERENT LOCATION
    > struct coord{
    > int x;
    > int y;
    > };
    > /* additional code goes here */
    > struct coord first, second;


    Yes.

    > ACCESSING STRUCTURE MEMBERS.
    > first.x = 50;
    > first.y = 100; /* to access a structure member you have to use the
    > dot (.) operator between */
    > /* the structure name and the member name */


    Yes.

    > WAY 3. TO DEDCLARE AND INITIALIZE A STRUCTURE.
    > struct time{
    > int hours;
    > int minutes;
    > int seconds;
    > }time_of_birth = {8,45,0};


    Yes.

    > TO COPY INFORMATION BETWEEN STRUCTURES.
    > first = second;
    > first = second;


    You only need to do it once...

    > the above is equivalent to
    > first.x = second.x;
    > first.y = second.y;


    Not necessarily. It may also be equivalent to

    memcpy(&first, &second, sizeof first);

    or, indeed, even stranger code. All you know is that after the copy, the
    values of the structure members are identical; the content of any
    padding bytes remains unspecified.

    > STRUCTURES THAT CONTAIN ARRAYS
    > struct data{
    > int x[4];
    > char y[10]
    > }record;
    >
    > record.y /* IS A POINTER TO THE FIRST ELEMENT OF THE ARRAY */


    No. It is the array. It has array type.

    > ARRAYS OF STRUCTURES
    > struct entry{
    > char fname[10];
    > char lname[12];
    > char phone[8];
    > };
    > struct entry list[100]; /* declaring an array of structures of type entry */
    > /* each element is a structure of type entry */
    > /* each element of list array has three elements */
    > /* IE: first element list[0] = list[0].fname,
    > list[0].lname, list[0].phone */
    > /* last element list[999] = list[999].fname,
    > list[999].lname, list[999].phoone */


    Yes.

    > INITIALIZING STRUCTURES
    >
    > struct customer{
    > char firm[20];
    > char contact[25];
    > };
    >
    > struct sale{
    > struct customer buyer; /* structure to a structure */
    > char item[20];
    > float amount;
    > };
    >
    > struct sale y1990[100] = { /* initializing the structures */
    > {{ "acme industries", "George Adams"},
    > "left-hand widget",
    > 1000.00
    > },
    > {{ "Wilson & co.","Ed Wilson"},
    > "Type 12 gizmo",
    > 200.00
    > }
    > };
    >
    > TO DISPLAY THE ABOVE STRUCTURES
    >
    > for(i = 0; i < 2; i++)
    > {
    > printf("%s %s\n %s £%.2f\n\n", y1990.buyer.firm,
    > y1990.buyer.contact, y1990.item, y1990.amount);
    > }


    Yes.

    > POINTERS AS STRUCTURE MEMBERS
    > struct data{
    > int *value;
    > int *rate
    > }first; /* You must initialize pointers to point at some
    > thing */
    > /* to do this you do as below */
    >
    > first.value = &cost;
    > first.rate = &interest; /* cost and interest must have been declared as int variables */


    Not necessarily. You could also use malloc(); or copy existing pointers
    to them; or, where appropriate, set them to null pointers.

    > /* When using char variable do not use the &
    > operator */


    Nope. When pointing a char * member at a _string_, do not use &. When
    pointing it at a single char, you still need the &.


    > POINTERS TO STRUCTURES
    > struct part{ /* define a structure */
    > int number;
    > char name[10];
    > };
    > struct part *p_part; /* declare a pointer to type part;
    > struct part gizmo; /* declare an instance to type part */
    > p_part = &gizmo; /* perform pointer initialization */
    >
    > (*p_part).number = 100; /* this assigns the value 100 to
    > gizmo.number*/
    > /* this uses the indirection operator (*)*/
    > p_part->number /* this is used to access structure members
    > using a pointer */
    > /* this uses the indirect membership operator
    > (->) */


    Note that those two are identical in all regards except spelling.

    > POINTERS AND ARRAYS OF STRUCTURES
    > struct part{ /* define struture */
    > int number;
    > char name[10];
    > };
    >
    > struct part data[100]; /* declare an array of type part */
    > struct part *p_part; /* declare a pointer to type part */
    > p_part = &data[0]; /* initialize pointer to point to first element in array */
    > p_part = data; /* second way to do above */


    Yes. Note lack of & operator in the second initialisation!

    > printf("%s %s", p_part->number, p_part->name);
    > /* to print contents of first element */


    No. That should be

    printf("%d %s", p_part->number, p_part->name);

    or something similar; p_part->number is an int, not a char *.

    > PASSING STRUCTURES AS ARGUMENTS TO FUNCTIONS
    > struct data{ /* define and initialize a structure */
    > float amount;
    > char fname[30];
    > char lname[30];
    > }rec;
    >
    > void print_rec(struct data x); /* declare function prototype*/
    > /* addditional statements go here */
    > print_rec(rec); /* call function */


    Yes.

    Richard
     
    Richard Bos, Jun 30, 2004
    #2
    1. Advertising

  3. Darklight

    pete Guest

    Darklight wrote:

    > WAY 3. TO DEDCLARE AND INITIALIZE A STRUCTURE.
    > struct time{
    > int hours;
    > int minutes;
    > int seconds;
    > }time_of_birth = {8,45,0};


    Could also be {8, 45} instead.

    --
    pete
     
    pete, Jun 30, 2004
    #3
  4. Darklight

    Al Bowers Guest

    Darklight wrote:
    > What is written below i will use as a quick reference i just want to make
    > sure i have got it right


    Bos has made a fine analysis of your quick reference sheet. I would
    suggest that you make an additional entry. Along with passing
    structures as arguments to funcions you should include:
    Passing sructure pointer as argument to functions. Instead of passing
    a copy of the structure it would probably be better to pass the
    pointer to the structure. This would be especially useful if the
    function is going to modify the structure. See example below.


    > PASSING STRUCTURES AS ARGUMENTS TO FUNCTIONS
    > struct data{ /* define and initialize a structure */
    > float amount;
    > char fname[30];
    > char lname[30];
    > }rec;
    >
    > void print_rec(struct data x); /* declare function prototype*/
    > /* addditional statements go here */
    > print_rec(rec); /* call function */
    >
    > void print_rec(struct data x) /* declare or define function definition */
    > {
    > printf("\nDoners name %s %s gave £%.2f\n",x.fname,
    > x.lname,x.amount);
    > }


    PASSING STRUCTURE POINTER AS ARGUMENT TO FUNCTIONS
    Function definition:
    void print_rec(const struct data *x)
    {
    printf("\nDoners name %s %s gave £%.2f\n",x->fname,
    x->lname,x->amount);
    return;
    }

    Function call:
    print_rec(&rec)

    Example:

    #include <string.h>
    #include <stdio.h>

    #define SZ_STR 30

    struct bio
    {
    char name[SZ_STR+1];
    unsigned age;
    };

    void setbio(struct bio *p, const char *name, unsigned age);
    void printbio(const struct bio *p);

    int main(void)
    {
    struct bio firstprez = {""}; /* no name, no age */

    printbio(&firstprez);
    puts("\nNow setting bio\n");
    setbio(&firstprez,"George Washington", 58);
    printbio(&firstprez);
    return 0;
    }

    void setbio(struct bio *p, const char *name, unsigned age)
    {
    strncpy(p->name,name,SZ_STR);
    p->name[SZ_STR] = '\0'; /* truncation of large name */
    p->age = age;
    return;
    }

    void printbio(const struct bio *p)
    {
    if(*p->name != '\0')
    printf("name: %s\n age: %u\n",p->name,p->age);
    else puts("No bio");
    return;
    }

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Jun 30, 2004
    #4
  5. Darklight

    Darklight Guest

    Darklight wrote:

    Thanks for that
     
    Darklight, Jun 30, 2004
    #5
  6. Darklight

    Jim Guest

    On Wed, 30 Jun 2004 12:18:51 GMT, (Richard
    Bos) wrote:

    >Darklight <> wrote:


    >> struct entry list[100]; /* declaring an array of structures of type entry */
    >> /* each element is a structure of type entry */
    >> /* each element of list array has three elements */
    >> /* IE: first element list[0] = list[0].fname,
    >> list[0].lname, list[0].phone */
    >> /* last element list[999] = list[999].fname,
    >> list[999].lname, list[999].phoone */

    >
    >Yes.


    Yes, but only if it was
    struct entry list[1000];

    Jim
     
    Jim, Jul 1, 2004
    #6
  7. On Wed, 30 Jun 2004 12:18:51 GMT, (Richard
    Bos) wrote:

    >Darklight <> wrote:
    >
    >> What is written below i will use as a quick reference i just want to make
    >> sure i have got it right
    >> /* STRUCTURES */
    >>
    >> TO DEFINE A STRUCTURE
    >> way 1. To declare a structure and instance/s together.
    >> struct coord{ /* struct keyword is used to declare a stucture, coord
    >> is the structure name or tag */
    >> int x;
    >> int y; /* x & y are the names of the member variables of the
    >> structure */
    >> } first, second; /* first & second are the instances of the structure */
    >> /* each instance contains two integers */

    >
    >Yes.
    >
    >> WAY 2. TO DECLARE INSTANCE IN A DIFFERENT LOCATION
    >> struct coord{
    >> int x;
    >> int y;
    >> };
    >> /* additional code goes here */
    >> struct coord first, second;

    >
    >Yes.
    >
    >> ACCESSING STRUCTURE MEMBERS.
    >> first.x = 50;
    >> first.y = 100; /* to access a structure member you have to use the
    >> dot (.) operator between */
    >> /* the structure name and the member name */

    >
    >Yes.
    >
    >> WAY 3. TO DEDCLARE AND INITIALIZE A STRUCTURE.
    >> struct time{
    >> int hours;
    >> int minutes;
    >> int seconds;
    >> }time_of_birth = {8,45,0};

    >
    >Yes.
    >
    >> TO COPY INFORMATION BETWEEN STRUCTURES.
    >> first = second;
    >> first = second;

    >
    >You only need to do it once...
    >
    >> the above is equivalent to
    >> first.x = second.x;
    >> first.y = second.y;

    >
    >Not necessarily. It may also be equivalent to
    >
    > memcpy(&first, &second, sizeof first);
    >
    >or, indeed, even stranger code. All you know is that after the copy, the
    >values of the structure members are identical; the content of any
    >padding bytes remains unspecified.
    >
    >> STRUCTURES THAT CONTAIN ARRAYS
    >> struct data{
    >> int x[4];
    >> char y[10]
    >> }record;
    >>
    >> record.y /* IS A POINTER TO THE FIRST ELEMENT OF THE ARRAY */

    >
    >No. It is the array. It has array type.


    True. However, in most expressions other than as the operand of the
    sizeof or & operators, it evaluates to the address of the first
    element with type pointer to element.

    >
    >> ARRAYS OF STRUCTURES
    >> struct entry{
    >> char fname[10];
    >> char lname[12];
    >> char phone[8];
    >> };
    >> struct entry list[100]; /* declaring an array of structures of type entry */
    >> /* each element is a structure of type entry */
    >> /* each element of list array has three elements */
    >> /* IE: first element list[0] = list[0].fname,
    >> list[0].lname, list[0].phone */
    >> /* last element list[999] = list[999].fname,
    >> list[999].lname, list[999].phoone */

    >
    >Yes.
    >
    >> INITIALIZING STRUCTURES
    >>
    >> struct customer{
    >> char firm[20];
    >> char contact[25];
    >> };
    >>
    >> struct sale{
    >> struct customer buyer; /* structure to a structure */
    >> char item[20];
    >> float amount;
    >> };
    >>
    >> struct sale y1990[100] = { /* initializing the structures */
    >> {{ "acme industries", "George Adams"},
    >> "left-hand widget",
    >> 1000.00
    >> },
    >> {{ "Wilson & co.","Ed Wilson"},
    >> "Type 12 gizmo",
    >> 200.00
    >> }
    >> };
    >>
    >> TO DISPLAY THE ABOVE STRUCTURES
    >>
    >> for(i = 0; i < 2; i++)
    >> {
    >> printf("%s %s\n %s £%.2f\n\n", y1990.buyer.firm,
    >> y1990.buyer.contact, y1990.item, y1990.amount);
    >> }

    >
    >Yes.
    >
    >> POINTERS AS STRUCTURE MEMBERS
    >> struct data{
    >> int *value;
    >> int *rate
    >> }first; /* You must initialize pointers to point at some
    >> thing */
    >> /* to do this you do as below */
    >>
    >> first.value = &cost;
    >> first.rate = &interest; /* cost and interest must have been declared as int variables */

    >
    >Not necessarily. You could also use malloc(); or copy existing pointers
    >to them; or, where appropriate, set them to null pointers.
    >
    >> /* When using char variable do not use the &
    >> operator */

    >
    >Nope. When pointing a char * member at a _string_, do not use &. When
    >pointing it at a single char, you still need the &.
    >
    >
    >> POINTERS TO STRUCTURES
    >> struct part{ /* define a structure */
    >> int number;
    >> char name[10];
    >> };
    >> struct part *p_part; /* declare a pointer to type part;
    >> struct part gizmo; /* declare an instance to type part */
    >> p_part = &gizmo; /* perform pointer initialization */
    >>
    >> (*p_part).number = 100; /* this assigns the value 100 to
    >> gizmo.number*/
    >> /* this uses the indirection operator (*)*/
    >> p_part->number /* this is used to access structure members
    >> using a pointer */
    >> /* this uses the indirect membership operator
    >> (->) */

    >
    >Note that those two are identical in all regards except spelling.
    >
    >> POINTERS AND ARRAYS OF STRUCTURES
    >> struct part{ /* define struture */
    >> int number;
    >> char name[10];
    >> };
    >>
    >> struct part data[100]; /* declare an array of type part */
    >> struct part *p_part; /* declare a pointer to type part */
    >> p_part = &data[0]; /* initialize pointer to point to first element in array */
    >> p_part = data; /* second way to do above */

    >
    >Yes. Note lack of & operator in the second initialisation!
    >
    >> printf("%s %s", p_part->number, p_part->name);
    >> /* to print contents of first element */

    >
    >No. That should be
    >
    > printf("%d %s", p_part->number, p_part->name);
    >
    >or something similar; p_part->number is an int, not a char *.
    >
    >> PASSING STRUCTURES AS ARGUMENTS TO FUNCTIONS
    >> struct data{ /* define and initialize a structure */
    >> float amount;
    >> char fname[30];
    >> char lname[30];
    >> }rec;
    >>
    >> void print_rec(struct data x); /* declare function prototype*/
    >> /* addditional statements go here */
    >> print_rec(rec); /* call function */

    >
    >Yes.
    >
    >Richard




    <<Remove the del for email>>
     
    Barry Schwarz, Jul 1, 2004
    #7
  8. Darklight

    Richard Bos Guest

    Barry Schwarz <> wrote:

    [ Jeez, man, learn to snip! ]

    > On Wed, 30 Jun 2004 12:18:51 GMT, (Richard
    > Bos) wrote:
    >
    > >Darklight <> wrote:
    > >
    > >> STRUCTURES THAT CONTAIN ARRAYS
    > >> struct data{
    > >> int x[4];
    > >> char y[10]
    > >> }record;
    > >>
    > >> record.y /* IS A POINTER TO THE FIRST ELEMENT OF THE ARRAY */

    > >
    > >No. It is the array. It has array type.

    >
    > True. However, in most expressions other than as the operand of the
    > sizeof or & operators, it evaluates to the address of the first
    > element with type pointer to element.


    Correct; however, IMO if you want to gain a real understanding of
    pointers, arrays, and those kinds of struct members, you do need to
    understand that an array is not a pointer, even though it often behaves
    as one.

    Richard
     
    Richard Bos, Jul 1, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Hubberstey
    Replies:
    0
    Views:
    1,094
    Tim Hubberstey
    Jul 3, 2003
  2. joon
    Replies:
    1
    Views:
    532
    Roedy Green
    Jul 8, 2003
  3. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    465
  4. J.Ram
    Replies:
    7
    Views:
    679
  5. froil
    Replies:
    12
    Views:
    324
    Gunnar Hjalmarsson
    Mar 2, 2006
Loading...

Share This Page