Defining elastic/flexible structure in C

后端 未结 2 1561
情歌与酒
情歌与酒 2021-01-26 14:22

I have a task to do and the content of the task is:

Please suggest a definition of linked list, which will keep the person\'s name and age in a flexible stru

2条回答
  •  执念已碎
    2021-01-26 15:05

    my guess: a flexible struct would be one that could handle any age and any name.

    A unsigned int field would handle any age (within reason).

    A char * field would handle any name.

    The struct itself would be:

    struct nameAge { unsigned int age;  char * pName; };  
    

    an instance of the struct would be:

    struct nameAge myNameAge;
    

    Setting the age field would be:

    myNameAge.age = ageValue;
    

    Setting the name field would be:

    myNameAge.name = malloc( numCharactersInName+1 );
    strcpy( myNameAge.name, nameString );
    

    How the code obtained the ageValue for age and/or the characters for NameString is up to the programmer to decide/implement.

提交回复
热议问题