通过引用将结构传递给将动态创建和填充结构数组的函数 [英] Passing a Structure by reference to a function that will dynamically create and fill a array of structures

查看:54
本文介绍了通过引用将结构传递给将动态创建和填充结构数组的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将结构指针传递给一个函数,该函数将在所传递的结构指针所指向的位置动态创建结构数组.我能够成功创建并填充结构数组,但是当尝试使用传递的指针在调用函数中打印数据时,会给我带来垃圾值.请帮助我知道为什么我的结构指针指向垃圾,以及如何正确访问数据.

I want to pass a structure pointer to a function that will dynamically create a array of structures at the location pointed to by the structure pointer that was passed. I am able to create and fill the array of structure successfully but when trying to print the data in the calling function using the pointer that was passed gives me a garbage values. Please help me know why my structure pointer is pointing to garbage and how can I access my data correctly.

以下仅是一些示例代码,用于演示如何使用malloc& amp;传递和动态填充结构.重新分配.这是不正确的方法:

The following is just some example code to demonstrate how the structure is passed and dynamically filled using malloc & realloc. this is INCORRECT method:

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

struct student 
{
    int id;
    char name[20];
    float percentage;
};

void func(struct student *record);

int main() 
{
    struct student *record = NULL;

    record = (struct student *)malloc(sizeof(struct student));

    func(record);

    if(record != NULL)
    {
        for(int i=0; i<2; i++)
        {
            printf(" 1 Id is: %d \n", record[i].id);
            printf(" 1 Name is: %s \n", record[i].name);
            printf(" 1 Percentage is: %f \n", record[i].percentage);
            printf("\n");
        }
    }
    else
    {
        printf("record pointer is null");
    }

    return 0;
}

void func(struct student *record1)
{
    for(int i=0; i<2; i++)
    {
        if(i)
        {
            record1 = (struct student *)realloc(record1,sizeof(struct student)*(i+1));
        }
        record1[i].id=1;
        strcpy(record1[i].name, "Raju");
        record1[i].percentage = 86.5;
    }
}

以下是使用双指针的类似示例,这是正确的方法:

The following is a similar example using double pointer which is the CORRECT way to do this:

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

struct student 
{
    int id;
    char name[20];
    float percentage;
};

void func(struct student **record);

int main() 
{
    struct student *record = NULL;

    func(&record);

    if(record != NULL)
    {
        for(int i=0; i<2; i++)
        {
            printf(" 1 Id is: %d \n", record[i].id);
            printf(" 1 Name is: %s \n", record[i].name);
            printf(" 1 Percentage is: %f \n", record[i].percentage);
            printf("\n");
        }
    }
    else
    {
        printf("record pointer is null");
    }

    free(record);

    return 0;
}

void func(struct student **record1)
{
    *record1 = (struct student *)malloc(sizeof(struct student));
    for(int i=0; i<2; i++)
    {
        if(i)
        {
            *record1 = (struct student *)realloc(*record1,sizeof(struct student)*(i+1));
        }
        (*record1)[i].id=1;
        strcpy((*record1)[i].name, "Raju");
        (*record1)[i].percentage = 86.5;
    }
}

推荐答案

您的第一个解决方案,

record1 = (struct student *)realloc(record1,sizeof(struct student)*(i+1));

只要重新分配不必移动指针,

就可以正常工作.也就是说,重新分配只是扩展了它先前提供给 record1 的内存区域.应该在以后的某个阶段要求 realloc 来给您另外一块内存,然后 main 中的较早指针 record 将变为无效,现在可以包含您的垃圾".

works as long as realloc does not have to move the pointer! That is, realloc just expands the memory area it gave earlier to record1. Should, at some later stage, realloc be required to give you another piece of memory, then the earlier pointer record in main will become invalid and could now contain your "garbage".

如您所想,您需要一个双指针才能在 main 中看到已更改的指针.您快到那里了,只是一个错字:

As you were thinking, you need a double pointer to be able to see the changed pointer in main. You were nearly there, just a typo:

*record1 = (struct student *)realloc(record1,sizeof(struct student)*(i+1));

上一行中的

还必须取消引用 record1 的第二次出现,因此 * record1 因为必须给 realloc 原始指针.

in the above line the second occurrence of record1 must also be dereferenced, so *record1 because you must give realloc the original pointer.

哦,不要强制转换malloc的结果!尽管编译器没有抱怨,但可能会导致将来出现问题.

Oh, and don't cast the result of malloc! Although the compiler does not complain, it can cause future problems.

这篇关于通过引用将结构传递给将动态创建和填充结构数组的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆