结构数组 - 如何正确创建它们

Array of structs - How to create them correctly

我正在学习结构和内存分配。

我通过研究发现的一件事是没有办法查看是否正确分配了内存块。

我认为它工作正常,但我正准备阅读有关链表的内容,但再次感到困惑。

我问这个,因为在链表上,我看到:

typedef LIST* Lint;

int main(){
    Lint *lint;
}

并在我的代码中定义 class 的类型:

typedef CLASS* t;

int main(){
    t class; // WITHOUT THE * BEFORE class
}

成功了!我认为这样定义就可以了。对我来说是有道理的,class 现在是一个指向类型的指针(类型即 CLASS);

那么,我是在正确的轨道上还是这次工作纯粹是运气?

代码结构糟透了,但我还在学习:)

typedef struct student {
    char    name[50];
    int     grade;
} TURMA;

typedef CLASS* t;

void showS(struct student i){
    printf("Name of Student: %s \n Grade of Student: %d\n", i.name, i.grade);
}

void insert(struct student *i,int k){
    char n[100]; int q=0;
    printf("Define name of student %d\n", k+1); scanf("%s", n);
    printf("Define the grade of %s\n", n); scanf("%d", &q);
    strcpy(i->name,n);
    i->grade = q;
}


int main()
{
    t class;
    int total,plus,i;
    printf("Define number of total students: \n"); scanf("%d", &total);
    class = (struct student*) malloc(total*(sizeof(CLASS)));

    for(i=0; i<total; i++){
        insert(&class[i], i);
    }
    printf("\n\nThis is the complete class: \n");
    for(i=0; i<total; i++){
        showS(class[i]);
    }

    printf("(Total size after malloc: %lu) Add more students (Int)\n",sizeof(*turma)); scanf("%d", &plus);
    class = realloc(class, plus*sizeof(CLASS));
    free(class);

    printf("(Total size after malloc: %lu)", sizeof(*class));
    return 0;
}

真的很难准确地说出你想要完成什么。我看起来你正在尝试从 struct student 中创建一个单链表,其中 typedefstruct studentTURMA。这就是有用信息结束的地方。试图声明 typedef CLASS* t; 是没有意义的。编译器不知道 CLASS 是什么。猜猜你在尝试什么,以下是你的代码的结果。

注意: 我添加了一个函数 flush_stdin 来清空 stdin 这样你就不会在缓冲区中留下讨厌的 newline 字符.我还更改了 scanf 格式字符串,以防止在读取字符串后留下换行符。看看下面,我会看看你的最新添加。

另请注意,在您的代码中,您不能 free (class); 然后期望采取 sizeof(*class)) -- 这是未定义的行为。

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

#define MAXN 100

typedef struct student {
    char    name[50];
    int     grade;
} TURMA;

typedef TURMA* t;

void flush_stdin () 
{
    int c = 0;
    while ((c = getchar()) != '\n' && c != EOF);
}

void showS (TURMA i)
{
    printf("Name of Student: %s \n Grade of Student: %d\n", i.name, i.grade);
}

void insert (t i, int k)
{
    char n[MAXN] = {0}; int q=0;

    printf("Define name of student %d\n", k+1); scanf ("%[^\n]%*c", n);
    printf("Define the grade of %s\n", n); scanf("%d", &q); flush_stdin();

    strcpy(i->name,n);
    i->grade = q;
}


int main()
{
    t class;
    int total,plus,i;

    printf("Define number of total students: \n"); scanf("%d", &total); flush_stdin();
    class = malloc (total * sizeof *class);
    if (!class) {
        fprintf (stderr, "error: virtual memory exhausted.\n");
        exit (EXIT_FAILURE);
    }

    for (i=0; i<total; i++){
        insert (&class[i], i);
    }

    printf("\n\nThis is the complete class: \n");
    for (i=0; i<total; i++){
        showS (class[i]);
    }

    printf("(Total size after malloc: %lu) Add more students (Int)\n", sizeof *class * total); 
    scanf("%d", &plus); flush_stdin();

    TURMA *turma = realloc (class, plus * sizeof *turma);
    if (!turma) {
        fprintf (stderr, "error: virtual memory exhausted.\n");
        exit (EXIT_FAILURE);
    }

    free (class);

    printf("(Total size after realloc: %lu)\n", sizeof *turma * (total + plus));

    free (turma);

    return 0;
}

输出

$ ./bin/llclass
Define number of total students:
2
Define name of student 1
John James
Define the grade of John James
8
Define name of student 2
Jill James
Define the grade of Jill James
9


This is the complete class:
Name of Student: John James
 Grade of Student: 8
Name of Student: Jill James
 Grade of Student: 9
(Total size after malloc: 112) Add more students (Int)
2
(Total size after realloc: 224)