Results 1 to 13 of 13

Thread: Can initialize fields of a structure?

  1. #1
    carlos is offline Senior Member
    Join Date
    Mar 2009
    Posts
    145
    Rep Power
    4

    Default Can initialize fields of a structure?

    For example:
    typedef struct
    (int a = 0;
    int b = 1;
    x);

    Does it work?

  2. #2
    macrona is offline Senior Member
    Join Date
    Mar 2009
    Posts
    116
    Rep Power
    4

    Default

    No, x is a shape, not a value.

    You can initialize the instantiation:

    Code:
    1.	ox = x (0,1);
    Now, if you want that whenever you create an x it is automatically initialized with a = 0, b = 1 ...

    ... then it is time to move to C + + which provides manufacturers made to it:

    Code:
    1.	struct x (
    2.	int a;
    3.	int b;
    4.	x () (
    5.	a = b = 0;
    6.	)7.	);

  3. #3
    carlos is offline Senior Member
    Join Date
    Mar 2009
    Posts
    145
    Rep Power
    4

    Default

    Ok thank you!

  4. #4
    elzer25 is offline Senior Member
    Join Date
    Mar 2009
    Posts
    119
    Rep Power
    4

    Default

    No, x is a shape, not a value.

    You can initialize the instantiation:

    Code:
    ox = x (0,1);
    Now, if you want that whenever you create an x it is automatically initialized with a = 0, b = 1 ...
    ... then it is time to move to C + + which provides manufacturers made to it:

    Code:
    struct x (
    int a;
    int b;
    x () (
    a = b = 0;
    )
    );
    Basically is there a difference between struct and class, made by COMPLÔ C + +?

  5. #5
    carlos is offline Senior Member
    Join Date
    Mar 2009
    Posts
    145
    Rep Power
    4

    Default

    It should be an encapsulation of history ...

    If I have another question.
    I still have my structure X:
    typedef struct
    (int a;
    int b;
    x);

    And I want to know if we can compare two instances of X:
    X foo;
    X bar;
    foo.a = 1;
    toto.b = 2;
    titi.a = 0;
    titi.b = 5;
    Is it possible to:
    if (foo == titi) {...}

  6. #6
    sanita is offline Senior Member
    Join Date
    Mar 2009
    Posts
    107
    Rep Power
    4

    Default

    Default (if nothing is specified) is private in a class and public in a structure, right?

  7. #7
    carlos is offline Senior Member
    Join Date
    Mar 2009
    Posts
    145
    Rep Power
    4

    Default

    I have another question (I feel I have forgotten everything).
    If I change my toto, is it better to move the pointer argument in toto or return another X:

    void modify (X * foo)
    (foo-> a = 54

    or

    X modify (foo X)
    (X temp;
    temp = foo;
    temp = 54;
    return temp;
    )

  8. #8
    elzer25 is offline Senior Member
    Join Date
    Mar 2009
    Posts
    119
    Rep Power
    4

    Default

    Ok but now I work in C so I can not redefine it if?

    There's a solution to the so-called hard:
    It works very well for your example!

    Code:
    Ttoto a;
    Ttoto b;
    [...]
    if (memcmp (a, b, sizeof (ttoto) == 0)
    (
    )

  9. #9
    elzer25 is offline Senior Member
    Join Date
    Mar 2009
    Posts
    119
    Rep Power
    4

    Default

    I have another question (I feel I have forgotten everything).

    If I change my toto, is it better to move the pointer argument in toto or return another X:

    void modify (X * foo)
    (foo-> a = 54

    or

    X modify (foo X)
    (X temp;
    temp = foo;
    temp = 54;
    return temp;
    )

    Me in this case I pass a pointer to the instance of foo in parmetre in case I want to change fields. AND I return the transaction status: OK or ERROR.

    Code:
    Ttoto a;
    int initialize (void * foo, size)
    (
    if (foo == NULL) return ERROR;
    if (size <1) return ERROR;
    memset (foo, 0, size);
    return OK;
    )
    if (initialize (& a, sizeof (ttoto))! = OK)
    (
    fprintf (stderr, "There's a testicle.  n");
    return or exit ...
    )

  10. #10
    tenzin is offline Senior Member
    Join Date
    Mar 2009
    Posts
    108
    Rep Power
    4

    Default

    Code:
    if (memcmp (& foo, & titi, sizeof (X) == 0) / * identical * /;
    But if there are holes in the structure because of alignments is not guaranteed.
    There remains a macro:

    Code:
    # define Xequal (x1, x2) ((x1). a == (x2). a & & (x1). b == (x2). b)
    You do not want to move to C + + where possible?

    Code:
    If I change my toto, is it better to move the pointer argument in toto or return another X:
    If foo is large, it is faster to pass by address.
    If foo has many members to change, it is faster to pass / return by value.
    If the function should be used in expressions, it is better to return by value.
    Me, I prefer by address (reference C + +).

  11. #11
    adlina is offline Senior Member
    Join Date
    Mar 2009
    Posts
    106
    Rep Power
    4

    Default

    No, x is a shape, not a value.

    You can initialize the instantiation:

    Code:
    ox = x (0,1);
    Now, if you want that whenever you create an x it is automatically initialized with a = 0, b = 1 ...
    ... then it is time to move to C + + which provides manufacturers made to it:

    Code:
    struct x (
    int a;
    int b;
    x () (
    a = b = 0;
    )
    );
    For people who have more than 2 hours of C + + in their lives, they can put x (): a (0), b (1) ()
    is much more elegant and avoids initializing ca 2 times the value.

    edit: and by the way, a = b = 0 is not terrible either, it's downright pig.

  12. #12
    elzer25 is offline Senior Member
    Join Date
    Mar 2009
    Posts
    119
    Rep Power
    4

    Default

    how can we initialize a table structure in C
    eg
    struct text
    (char id [20];
    char pw [20];
    );
    typdef struct text;
    text table [100];

  13. #13
    sanita is offline Senior Member
    Join Date
    Mar 2009
    Posts
    107
    Rep Power
    4

    Default

    text table [100] = (( "lol" "rofl"), .... , ( "Test", "test));

Similar Threads

  1. How to Insert Form Fields Into Web Pages
    By ThomasBarnes in forum Software Jargons
    Replies: 0
    Last Post: 03-02-2010, 03:23 PM
  2. (C) + + Table Structure
    By willi in forum Programming
    Replies: 4
    Last Post: 11-16-2009, 11:57 AM
  3. Mozilla Firefox 3.0 Spell check Input Fields
    By unerina in forum General Internet Terms
    Replies: 0
    Last Post: 07-12-2008, 06:48 PM
  4. Spell-check input fields
    By fimanson in forum General Software Terms
    Replies: 0
    Last Post: 06-24-2008, 05:02 PM
  5. Sound structure
    By wenssal in forum Web. 2.0
    Replies: 0
    Last Post: 06-21-2008, 06:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48