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

C 结构数组-如何正确创建它们,c,struct,C,Struct,我正在学习结构和内存分配 我通过研究发现,没有一种方法可以判断内存块的分配是否正确 我认为它工作正常,但我正准备阅读有关链表的内容,又一次感到困惑 我问这个问题,因为在链表上,我看到: typedef LIST* Lint; int main(){ Lint *lint; } typedef CLASS* t; int main(){ t class; // WITHOUT THE * BEFORE class } 通过在我的代码中定义这样的类类型: typedef LI

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

我通过研究发现,没有一种方法可以判断内存块的分配是否正确

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

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

typedef LIST* Lint;

int main(){
    Lint *lint;
}
typedef CLASS* t;

int main(){
    t class; // WITHOUT THE * BEFORE class
}
通过在我的代码中定义这样的类类型:

typedef LIST* Lint;

int main(){
    Lint *lint;
}
typedef CLASS* t;

int main(){
    t class; // WITHOUT THE * BEFORE class
}
成功了!我想这样定义是可以的。对我来说,有意义的是,类现在是指向一个类型(类型即类)的指针

那么,我是走对了路还是这项工作纯属运气?

代码结构很糟糕,但我还在学习:)

typedef结构学生{
字符名[50];
国际等级;
}图尔马;
类型定义类别*t;
无效显示(结构学生i){
printf(“学生姓名:%s\n学生成绩:%d\n”,即姓名,即成绩);
}
无效插入(结构学生*i,整数k){
字符n[100];int q=0;
printf(“定义学生的名字%d\n”,k+1);scanf(“%s”,n);
printf(“定义%s\n”,n)的等级;scanf(“%d”,&q);
strcpy(i->name,n);
i->等级=q;
}
int main()
{
t级;
整数总计,加上i;
printf(“定义学生总数:\n”);scanf(“%d”,&total);
class=(struct student*)malloc(total*)(sizeof(class));

对于(i=0;i很难准确地说出您想要完成的任务。我看起来您正试图从
struct student
创建一个单独的链表,其中
typedef
struct student
to
TURMA
。这就是有用信息的结尾。试图声明
typedef CLASS*t
毫无意义。编译器不知道什么是
。猜猜你在尝试什么,下面是你的代码流

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

还要注意,在代码中,您不能
释放(类);
然后期望获得
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;
}

如果malloc失败,它将返回一个空指针don't cast malloc:
class=malloc(total*(sizeof(class));
是所需的全部。(假定
class
是正确的。)另外,只有在对数据
类型使用
sizeof
时才需要括号(例如
sizeof(int)
),而不是在获取变量大小时。
sizeof(*class)
可以是
sizeof*class
为什么他们使用
Lint*Lint
而不是
Lint-Lint
?请注意我的
t class
,如果是
t*class
?最后一个选项将使指针类成为指向struct student的指针?不,因为
typedef class*t
使
t
成为
class*
(即指向
class
)的指针@skills好吧,你有问题,但是
typedef Lint
允许创建
Lint
类型的指针,指向链接列表中的下一个节点。(例如
Lint*Lint;
)。但是,除非您定义
是什么,否则您将永远无法达到这一点。您正在尝试从uknown类型为
t
创建
typedef
。我们需要您的更多代码。请参阅:。“谢谢你的见解David,代码已经在运行了,它没有编译的原因是因为我用葡萄牙语编写代码,在这里发布之前,我尝试将所有术语翻译成英语。但是很高兴看到你所做的更改,现在变得更有意义了。非常感谢。