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

时间:2015-05-09 00:15:18

标签: c struct

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

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

我认为它运行正常,但我正准备阅读有关链接列表的内容,并再次感到困惑。

我问这个,因为在链接列表中,我看到了:

typedef LIST* Lint;

int main(){
    Lint *lint;
}

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

typedef CLASS* t;

int main(){
    t class; // WITHOUT THE * BEFORE 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;
}

1 个答案:

答案 0 :(得分:2)

很难确切地说出你想要完成什么。我看起来正在尝试使用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)