Structer指针和指针char数组malloc数组

时间:2016-10-24 13:21:32

标签: c pointers malloc structure realloc

我想做结构数组,但我不知道结构数组大小,因此我需要使用指针结构,我想在结构中做char数组,因此我不知道字符数组大小因此我需要在这个结构中使用指针char,但我不了解malloc和realloc函数。我怎样才能做到这一点 ?

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

struct School{
    char *school_name;
    int student_size;
}*high_school;




void createSchool(struct School *s, char *schl_name, int student, int school_size)
{
    int i = 0;

    if(school_size == 1){
        s = (struct School*) malloc(sizeof(struct School));
    }
    else{
        s = (struct School*) realloc(s, (school_size*sizeof(struct School)));
    }

    (s+(school_size-1))->student_size = student;
    (s+(school_size-1))->school_name = (char *) malloc(20); // 20 it is not important

    (s+(school_size-1))->school_name = schl_name;
     for(i; i<school_size; i++){
        printf("%s\t%d\n",(s+i)->school_name, (s+i)->student_size);
    }
    printf("\n\n");
}

int main()
{
    int i = 1;
    createSchool(high_school, "Harvard", 50, i);
    i++;
    createSchool(high_school, "Oxford", 40, i);
    i++;
    createSchool(high_school, "MIT", 30, i);
}

我想做屏幕拍摄:

Harvard 50

Harvard 50
Oxford 40

Harvard 50
Oxford 40
MIT 30

但节目的截屏:

Harvard 50


└1q     7405760
Oxford  40


        7405760
(null)  0
MIT     30

1 个答案:

答案 0 :(得分:0)

createSchool内的指针具有局部范围,因此不会修改全局指针。更快的修复方法是将新分配的内存返回给调用者。

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

struct School{
    char *school_name;
    int student_size;
}*high_school;

struct School* createSchool(struct School *s, char *schl_name, int student, int school_size)
{

    if(school_size == 1)
    {
        s = malloc(sizeof(struct School));
    }
    else
    {
        s = realloc(s, (school_size*sizeof(struct School)));
    }

    if (s != NULL)
    {
        s[school_size-1].student_size = student;
        s[school_size-1].school_name = malloc(strlen(schl_name)+1);

        strcpy(s[school_size-1].school_name, schl_name);

        for(int i=0; i<school_size; i++)
        {
            printf("%s\t%d\n", s[i].school_name, s[i].student_size);
        }
        printf("\n\n");
    }

    return s;
}

int main(void)
{
    int i = 1;
    high_school = createSchool(high_school, "Harvard", 50, i);
    i++;
    high_school = createSchool(high_school, "Oxford", 40, i);
    i++;
    high_school = createSchool(high_school, "MIT", 30, i);
}
  1. main的最小签名是int main (void)
  2. 请注意,必须检查malloc / realloc的返回值。
  3. 使用您的代码,如果realloc失败,您将丢失指向已分配内存的指针。因此,您应该使用临时指针来存储realloc结果并检查完整性。之后你可以重新分配你的指针。
  4. struct School* createSchool(struct School *s, char *schl_name, int student, int school_size)
    {
    
        if(school_size == 1){
            s = malloc(sizeof(struct School));
        }
        else
        {
            struct School *temp = realloc(s, (school_size*sizeof(struct School)));
    
            if (temp == NULL)
                return s;
    
            s = temp;    
        }
    
        if (s != NULL)
        {
            s[school_size-1].student_size = student;
            s[school_size-1].school_name = malloc(strlen(schl_name)+1);
    
            strcpy(s[school_size-1].school_name, schl_name);
    
            for(int i=0; i<school_size; i++)
            {
                printf("%s\t%d\n", s[i].school_name, s[i].student_size);
            }
            printf("\n\n");
        }
    
        return s;
    }
    

    可以使用双指针实现不同的解决方案:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct School{
        char *school_name;
        int student_size;
    }*high_school;
    
    void createSchool(struct School **s, char *schl_name, int student, int school_size)
    {
    
        if(school_size == 1)
        {
            *s = malloc(sizeof(struct School));
        }
        else
        {
            struct School *temp = realloc(*s, (school_size*sizeof(struct School)));
    
            if (temp == NULL)
                return;
    
            *s = temp;
        }
    
        if (*s != NULL)
        {
            (*s)[school_size-1].student_size = student;
            (*s)[school_size-1].school_name = malloc(strlen(schl_name)+1);
    
            strcpy((*s)[school_size-1].school_name, schl_name);
    
            for(int i=0; i<school_size; i++)
            {
                printf("%s\t%d\n", (*s)[i].school_name, (*s)[i].student_size);
            }
            printf("\n\n");
        }
    }
    
    int main(void)
    {
        int i = 1;
        createSchool(&high_school, "Harvard", 50, i);
        i++;
        createSchool(&high_school, "Oxford", 40, i);
        i++;
        createSchool(&high_school, "MIT", 30, i);
    }
    

    最后要注意的是,要指定学校名称,您只需使用:

        (*s)[school_size-1].school_name = schl_name;