How do I use a structure?

前端 未结 3 658
执念已碎
执念已碎 2020-12-12 07:33

Ok firstly I\'ll explain my assignment. For this assignment I have to use dynamic memory allocation which I am having no problems with. What I am having a problem with is fi

3条回答
  •  抹茶落季
    2020-12-12 08:03

    To see the task it is better at least for the first time to write some block-scheme of what you have to do in a program. In your case:

    1. Read data from user (each structure).
    2. Increase array size, add new structure.
    3. Loop 1-2 until input user finish adding new people (needs some condition here to finish).
    4. Find necessary structure and print it.

    So the first step is to read information from user. You can use scanf(): In the simplest way you can do that step-by-step for each field:

    #include 
    ...
    int value;
    scanf("%d", &value);
    ...
    

    In case of success this function should return number of items it reads (1 in your case). For phone you should use scanf("%ld", &phone).

    To resize array use function realloc() (#include :

    realloc(&ptr_to_array, new_size);
    

    Each elements of the array is a pointer to structure "student". Next steps are similar.

提交回复
热议问题