Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Advanced data representation

Main Contents:

Function: learn more about malloc ()

Use C for different types of data

New algorithms, conceptually enhance the ability to develop programs

Abstract Data Types (ADT)

Learning computer languages is like learning music, carpentry, or engineering. First, learn how to use tools: learn how to play scales, such as using a hammer, and then solve problems such as landing, sliding, and balancing objects. So far, the reader has been learning and practicing various programming skills in this book, such as creating variables, structures, functions, etc. However, if you want to move to the next level, the tools are secondary and the real challenge is to design and create a project.

You should understand the built-in types of C by now

Simple variables, arrays, Pointers, structures, and unions

We’re going to talk about algorithms, ways of manipulating data

Then we’ll look at the process of designing data types, which is the process of matching algorithms with data, covering common data forms such as queues, lists, and binary trees

Small litchi

#include <stdio.h>
#include <string.h>
#define TSIZE 45    // Store a partial array size
#define FMAX 5      // The maximum number of movies

struct film
{
    char title[TSIZE];
    int rating;
};

char * s_gets(char str[] , int lim);    // A pointer to a function type

int main(void)
{
    struct film movies[FMAX];   // Define an array of structures in which each element is a film structure variable
    int i = 0;
    int j;
    
    puts("Enter first movie title:");
    while(i < FMAX && s_gets(movies[i].title,TSIZE) ! =NULL && movies[i].title[0] != '\ 0')
    {
        puts("Enter your rating<0-10>:");
        scanf("%d", &movies[i++].rating);
            while(getchar() ! ='\n')
                continue;
            puts("Enter next movie title (empty line to stop):");
    }
    if (i == 0)
        printf("No data entered.");
    else
        printf("Here is the movie list:\n");

    for (j = 0; j < i; j++)
        printf("Movie: %s Rating: %d\n",movies[j].title,movies[j].rating);
    printf("Bye! \n");

    return 0;
}

char * s_gets(char * st,int n)
{
    char * ret_val;
    char * find;

    ret_val = fgets(st,n,stdin);
    if (ret_val)
    {
        find = strchr(st,'\n');
        if (find)
            *find = '\ 0';
        else
            while(getchar() ! ='\n')
                continue;
    }
    returnret_val; } PS D:\Code> CD"D :\Code\C\ structure \"; if ($?) { gcc structDemo03.c -o structDemo03 } ; if ($?) { .\structDemo03 } Enter first movie title: Roman holiday Enter your rating<0-10>: 2 Enter next movie title (empty line to stop): Here is the movie list: Movie: Roman holiday Rating: 2 Bye!Copy the code

Program parsing

The program creates a structured array and stores the data entered by the user in the array. Input is not terminated until the array is full (judged by FMAX) or until the end of the file is reached (judged by NULL), or until the user presses Enter (judged by ‘\0’) on the first line. This is a bit of a problem with programming. First, the program is likely to waste a lot of space, since most titles are no longer than 40 characters. Still, some titles are indeed long, such as The Discreet Charm of The Bourgeoisie and Won Ton Ton, The Dog Who SavedHollywood. Secondly, many people would feel that the limit of five films a year is too strict. Of course, you can relax the limit, but how much? Some people can watch 500 movies a year, so they can change their FMAX to 500. However, for some people, this may still not be enough, and for others there are simply too many movies to watch in a year, wasting a lot of memory. In addition, some compilers set a default limit on the amount of memory available to automatically store class variables (movies, for example), and such large arrays may exceed the default values. You can solve this problem by declaring the array as static or external, or by setting up the compiler to use a larger stack. But. that doesn’t solve the problem. The real problem with this program is that the data representation is too inflexible. It is better to determine the amount of memory a program needs at run time than at compile time. To solve this problem, you should use dynamic memory allocation to represent data. It can be done like this:

The updated program does not determine the number of elements in the array before the program runs, which is a call to allocate contiguous memory space.

Create a pointer to this structure, and then assign an address to the pointer (n*sizeof(struct film))