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

标签 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;
}

最佳答案

很难准确地说出您要完成的任务。我看起来像你正在尝试用 struct studentTURMA 的 typedefstruct student 创建一个单链表。这就是有用信息结束的地方。尝试声明 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)

关于c - 结构数组 - 如何正确创建它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30134613/

相关文章:

C编程: void* - Why not parametric polymorphism?

c - 使用 uint64_t 32 位和 64 位架构中的结构填充差异

C# - 在结构中强制执行子对象不变性

c++ - C++ 中兼容的可变长度结构

c++ - 比较来自外部 API 的相同类型的两个结构

java - 优化 SSE 代码

c - 嵌入式linux调用使用系统

在函数中复制结构体并在c中返回副本

c - 如果我想对指针进行常规运算(不是指针运算),我应该使用 uintptr_t 吗?

c - 将二维数组传递给c中的函数时出错