繁体   English   中英

如何动态分配结构+指针

[英]How to dynamically allocate an struct + pointer

我一直在尝试找出如何在结构数组中存储来自用户的信息,但是到目前为止……不起作用。

我创建了该结构,并在主体内部创建了指向该结构的指针,然后动态分配了该结构。 但是我真的不知道如何从用户那里获取信息。 我知道,但是没有按预期工作。 如果我只使用一个结构体数组,它将是这样的……

&p [i] .id //正常

我尝试使用此方法,但是不起作用,idk为什么...代码尚未完成...

//
//  7.c
//  IFTM Exercises
//
//  Created by Lelre Ferreira on 8/29/19.
//  Copyright © 2019 Lelre Ferreira. All rights reserved.
//

#define size 5
#include <stdio.h>
#include <stdlib.h>

struct produtos {

    int id;
    int quant;
    float vlrs;
    char nm[50];

};

void cadastroProdutos (struct produtos *p, int tamanho);
void maiorValorProdutos (struct produtos *p, int tamanho);
void maiorEstoqueProdutos (struct produtos *p, int tamanho);

int main (int argc, const char * argv[]){

    struct produtos *p;
    int tamanho    = 0;

    printf("Insira quantidade de produtos/itens a serem cadastrados: ");
    scanf("%d", &tamanho);
    p = (struct produtos *) malloc(tamanho   * sizeof(struct produtos));

    cadastroProdutos(p, tamanho);
    maiorValorProdutos(p, tamanho);
    maiorEstoqueProdutos(p, tamanho);

    return 0;
}

void cadastroProdutos(struct produtos *p, int tamanho){

    int i = 0;
    for (i = 0; i < tamanho; i++) {

        printf("Insira o ID: ");
        scanf("%d", &p[i] -> id);
        printf("Insira o nome: ");
        scanf("%s",  p[i] -> nm);
        printf("Insira o valor: ");
        scanf("%f", &p[i] -> vlrs);
        printf("Insira a quantidade: ");
        scanf("%d", &p[i] -> quant);

    }
}

void maiorValorProdutos(struct produtos *p, int tamanho){



}

void maiorEstoqueProdutos(struct produtos *p, int tamanho){



}

IDE出现此错误:无法接受类型为'int'的右值的地址...

p[i]struct produtos ,而不是struct produtos struct productos* 也就是说,您可以使用来访问其成员. 运算符,不是->

printf("Insira o ID: ");
scanf("%d", &p[i].id);
printf("Insira o nome: ");
scanf("%s",  p[i].nm);
printf("Insira o valor: ");
scanf("%f", &p[i].vlrs);
printf("Insira a quantidade: ");
scanf("%d", &p[i].quant);

这与将p定义为main的数组没有什么不同。 当您将数组传递给函数时,它会衰减为指向其第一个元素的指针。 例如:

// This passes a pointer to the first element of an
// array of produtos to cadastroProdutos
struct produtos p1[5];
cadastroProdutos(p1, 5);
// This also passes a pointer to the first element
// of an array of produtos to cadastroProdutos
struct produtos* p2 = malloc(5 * sizeof(struct produtos));
cadastroProdutos(p2, 5);

cadastroProdutos函数的角度来看,这两个调用是完全相同的。 在这两种情况下,它仅接收一个指向数组第一个元素和数组大小的指针。

您可能错过了理解,应用于指针p[..]运算符(例如p[0] )充当了对struct produtos'.'类型的引用'.' 适当的运算符(而不是@MilesBudnek的答案中指出的struct produtos*

除了您的'.' '->'运算符问题,您似乎缺少动态分配的要点。 #define size 5分配#define size 5结构开始是可以的,但动态分配内存的目的是能够通过根据需要重新分配额外的内存来处理所有输入(或直到用尽)来无缝处理第6个结构可用内存)。

当前编写函数cadastroProdutos的方式并不能真正实现干净的动态分配(除了它返回void并且其中没有任何输入经过验证的事实)。 与其在函数中循环size时间,不如只是在每次调用cadastroProdutos获取值1结构的数据。 这样,您可以根据需要多次调用它,直到输入所有数据为止。 无需在函数内循环,并且对scanf的使用非常脆弱,因为您将无法处理由于匹配失败而导致的stdin遗留的字符,或者只是偶然输入了杂散字符(例如,为id键入的'a' ,或名称超过49字符,等等。)

要解决大多数问题,请不要对用户输入使用scanf ,而应将所有用户输入读入合理大小(例如1024字符左右)的临时缓冲区中,然后使用sscanf从缓冲区中解析所需的信息。 这具有双重好处,即可以分别验证读取和解析。 并且通过使用合理大小的缓冲区,您每次都会消耗一整行输入,从而消除了多余的字符问题(例如,踩在键盘上的猫)。

您的cadastroProdutos接受输入,没有提供有意义的返回值来指示输入成功/失败,如果任何一个输入失败,则函数容易受到未定义行为的影响 因此,通过检查所用功能的返回来验证每个用户输入。 任何输入故障,返回0 失败 ,并成功非零让您处理任何输入失败回到调用函数。

注意:计数类型应使用size_t而不是int

将这些更改汇总到cadastroProdutos ,您可以执行以下操作:

size_t cadastroProdutos (produtos *p, size_t *tamanho)
{
    produtos tmp;       /* temporary struct */
    char buf[MAXC];     /* temporary buffer */

    /* prompt, read-line, validate parse */
    fputs ("\nInsira o ID: ", stdout);
    if (!fgets (buf, MAXC, stdin) || sscanf (buf, "%d", &tmp.id) != 1)
        return 0;   /* (failure) */

    fputs ("Insira o nome: ", stdout);
    if (!fgets (buf, MAXC, stdin) || sscanf (buf, "%49[^\n]", tmp.nm) != 1)
        return 0;

    fputs ("Insira o valor: ", stdout);
    if (!fgets (buf, MAXC, stdin) || sscanf (buf, "%f", &tmp.vlrs) != 1)
        return 0;

    fputs ("Insira a quantidade: ", stdout);
    if (!fgets (buf, MAXC, stdin) || sscanf (buf, "%d", &tmp.quant) != 1)
        return 0;

    p[(*tamanho)++] = tmp;  /* assign tmp to p[*tamanho], increment */

    return *tamanho;    /* return tamanho (success) */
}

注意: tamanho作为指针传递,因此可以在函数中更新其编号,如果失败则返回0 ,而如果成功则返回更新的tamanho ,允许您有意义地返回size_t类型)

避免在代码中使用幻数 您已经很好地定义了一个常量作为size ,现在只需细化定义所需的常量,这样就不会再有像char nm[50];这样的幻数了 char nm[50]; 用它。 这样做可以使用更多的#define ,例如

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

#define PROD 2      /* initial no. of struct to allocate */
#define NMSZ 50     /* max name size */
#define MAXC 1024   /* reasonable no. of chars for temporary buffer */

typedef struct {    /* a typedef used for convenience */
    int id;
    int quant;
    float vlrs;
    char nm[NMSZ];
} produtos;

现在,您在main()需要做的就是声明一个临时缓冲区来容纳该行, size_t值,当前分配的结构总数( allocated )和填充的数量( tomanho ),从而可以在开始每次循环的if (tomanho == allocated) ,你知道你需要realloc试图填补更多的额外的前结构。 您的main()函数本质上可以是:

int main (void) {

    size_t  allocated = PROD,   /* initial number of struct to allocate */
            tomanho = 0;        /* number of allocated structs used */
    produtos *p = malloc (allocated * sizeof *p);   /* allocate */

    if (!p) {   /* validate EVERY allocation */
        perror ("malloc-p");
        return 1;
    }

    while (cadastroProdutos (p, &tomanho)) {    /* loop validating return */
        char buf[MAXC];                         /* buffer for input (y/n) */
        if (tomanho == allocated) { /* is a realloc needed to add struct? */
            /* always realloc with a temporary pointer */
            void *tmp = realloc (p, 2 * allocated * sizeof *p);
            if (!tmp) {     /* validate the reallocation */
                perror ("realloc-p");
                break;      /* realloc failed, original p still good, break */
            }
            p = tmp;        /* assign new block of mem to p */
            allocated *= 2; /* update no. of structs allocated */
        }

        fputs ("\n  add another (y/n)? ", stdout);  /* add more produtos? */
        if (!fgets (buf, MAXC, stdin) || !(*buf == 'y' || *buf == 'Y')) {
            putchar ('\n');
            break;
        }
    }

此时,您所有的结构都被填充并存储在p ,您所需要做的就是处理数据(下面简单地输出),然后对已分配的内存进行free() ,例如

    for (size_t i = 0; i < tomanho; i++)    /* loop showing stored data */
        printf ("p[%2zu] %4d  %-20s  %6.2f  %4d\n",
                i, p[i].id, p[i].nm, p[i].vlrs, p[i].quant);

    free (p);   /* don't forget to free memory you allocate */
}

简而言之,您可以执行以下操作:

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

#define PROD 2      /* initial no. of struct to allocate */
#define NMSZ 50     /* max name size */
#define MAXC 1024   /* reasonable no. of chars for temporary buffer */

typedef struct {    /* a typedef used for convenience */
    int id;
    int quant;
    float vlrs;
    char nm[NMSZ];
} produtos;

size_t cadastroProdutos (produtos *p, size_t *tamanho)
{
    produtos tmp;       /* temporary struct */
    char buf[MAXC];     /* temporary buffer */

    /* prompt, read-line, validate parse */
    fputs ("\nInsira o ID: ", stdout);
    if (!fgets (buf, MAXC, stdin) || sscanf (buf, "%d", &tmp.id) != 1)
        return 0;   /* (failure) */

    fputs ("Insira o nome: ", stdout);
    if (!fgets (buf, MAXC, stdin) || sscanf (buf, "%49[^\n]", tmp.nm) != 1)
        return 0;

    fputs ("Insira o valor: ", stdout);
    if (!fgets (buf, MAXC, stdin) || sscanf (buf, "%f", &tmp.vlrs) != 1)
        return 0;

    fputs ("Insira a quantidade: ", stdout);
    if (!fgets (buf, MAXC, stdin) || sscanf (buf, "%d", &tmp.quant) != 1)
        return 0;

    p[(*tamanho)++] = tmp;  /* assign tmp to p[*tamanho], increment */

    return *tamanho;    /* return tamanho (success) */
}

int main (void) {

    size_t  allocated = PROD,   /* initial number of struct to allocate */
            tomanho = 0;        /* number of allocated structs used */
    produtos *p = malloc (allocated * sizeof *p);   /* allocate */

    if (!p) {   /* validate EVERY allocation */
        perror ("malloc-p");
        return 1;
    }

    while (cadastroProdutos (p, &tomanho)) {    /* loop validating return */
        char buf[MAXC];                         /* buffer for input (y/n) */
        if (tomanho == allocated) { /* is a realloc needed to add struct? */
            /* always realloc with a temporary pointer */
            void *tmp = realloc (p, 2 * allocated * sizeof *p);
            if (!tmp) {     /* validate the reallocation */
                perror ("realloc-p");
                break;      /* realloc failed, original p still good, break */
            }
            p = tmp;        /* assign new block of mem to p */
            allocated *= 2; /* update no. of structs allocated */
        }

        fputs ("\n  add another (y/n)? ", stdout);  /* add more produtos? */
        if (!fgets (buf, MAXC, stdin) || !(*buf == 'y' || *buf == 'Y')) {
            putchar ('\n');
            break;
        }
    }

    for (size_t i = 0; i < tomanho; i++)    /* loop showing stored data */
        printf ("p[%2zu] %4d  %-20s  %6.2f  %4d\n",
                i, p[i].id, p[i].nm, p[i].vlrs, p[i].quant);

    free (p);   /* don't forget to free memory you allocate */
}

注意: add another (y/n)?了一个简单的add another (y/n)?提示以确定用户是否要添加更多数据)

使用/输出示例

上面我们从2个分配的结构开始,然后继续输入6个结构,强制进行重新分配。 例如:

$ ./bin/produtos

Insira o ID: 1
Insira o nome: John Brown
Insira o valor: 12.34
Insira a quantidade: 415

  add another (y/n)? y

Insira o ID: 2
Insira o nome: Mary Brown
Insira o valor: 23.45
Insira a quantidade: 416

  add another (y/n)? y

Insira o ID: 3
Insira o nome: Mickey Mouse
Insira o valor: 34.56
Insira a quantidade: 417

  add another (y/n)? y

Insira o ID: 4
Insira o nome: Minnie Mouse
Insira o valor: 45.67
Insira a quantidade: 418

  add another (y/n)? y

Insira o ID: 5
Insira o nome: Sam Clemens
Insira o valor: 56.78
Insira a quantidade: 419

  add another (y/n)? y

Insira o ID: 6
Insira o nome: Mark Twain
Insira o valor: 67.89
Insira a quantidade: 420

  add another (y/n)? n

p[ 0]    1  John Brown             12.34   415
p[ 1]    2  Mary Brown             23.45   416
p[ 2]    3  Mickey Mouse           34.56   417
p[ 3]    4  Minnie Mouse           45.67   418
p[ 4]    5  Sam Clemens            56.78   419
p[ 5]    6  Mark Twain             67.89   420

所有数据都已正确存储,如果需要,您可以添加1000个以上的条目。 您还应该通过内存错误检查程序(例如Linux上的valgrind )运行任何使用动态内存的程序,该程序可以判断您是否滥用了您不拥有的内存块指针。

一个简短的测试可以重定向文件中的输入而不是重新输入,可以确认是否存在任何内存问题,并且使用内存检查器非常简单,尽管可以运行程序,例如

$ valgrind ./bin/produtos < dat/produtos.txt
==12885== Memcheck, a memory error detector
==12885== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==12885== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==12885== Command: ./bin/produtos
==12885==

Insira o ID: Insira o nome: Insira o valor: Insira a quantidade:
  add another (y/n)?
Insira o ID: Insira o nome: Insira o valor: Insira a quantidade:
  add another (y/n)?
Insira o ID: Insira o nome: Insira o valor: Insira a quantidade:
  add another (y/n)?
Insira o ID: Insira o nome: Insira o valor: Insira a quantidade:
  add another (y/n)?
Insira o ID: Insira o nome: Insira o valor: Insira a quantidade:
  add another (y/n)?
Insira o ID: Insira o nome: Insira o valor: Insira a quantidade:
  add another (y/n)? 
p[ 0]    1  John Brown             12.34   415
p[ 1]    2  Mary Brown             23.45   416
p[ 2]    3  Mickey Mouse           34.56   417
p[ 3]    4  Minnie Mouse           45.67   418
p[ 4]    5  Sam Clemens            56.78   419
p[ 5]    6  Mark Twain             67.89   420
==12885==
==12885== HEAP SUMMARY:
==12885==     in use at exit: 0 bytes in 0 blocks
==12885==   total heap usage: 3 allocs, 3 frees, 896 bytes allocated
==12885==
==12885== All heap blocks were freed -- no leaks are possible
==12885==
==12885== For counts of detected and suppressed errors, rerun with: -v
==12885== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

始终确认"All heap blocks were freed -- no leaks are possible" ,并且没有错误。

仔细检查一下,如果您还有其他问题,请告诉我。

运行它,查看您将了解的代码

//
//  7.c
//  IFTM Exercises
//
//  Created by Lelre Ferreira on 8/29/19. Fix by Mr. J CHEN
//  Copyright © 2019 Lelre Ferreira. All rights reserved.
//

#define size 5
#include <stdio.h>
#include <stdlib.h>

struct produtos {

    int id;
    int quant;
    float vlrs;
    char nm[50];

};

void cadastroProdutos (struct produtos **p, int tamanho);
void maiorValorProdutos (struct produtos **p, int tamanho);
void maiorEstoqueProdutos (struct produtos **p, int tamanho);

int main (int argc, const char * argv[]){

    struct produtos **p;
    int tamanho    = 0,i;

    printf("Insira quantidade de produtos/itens a serem cadastrados: ");
    scanf("%d", &tamanho);
    p = (struct produtos **) malloc(tamanho   * sizeof(struct produtos*));
    for(i=0;i<tamanho;i++){
        p[i]=(struct produtos *) malloc(tamanho   * sizeof(struct produtos));
    }
    cadastroProdutos(p, tamanho);
    maiorValorProdutos(p, tamanho);
    maiorEstoqueProdutos(p, tamanho);
    for(i=0;i<tamanho;i++){
        free(p[i]);
    }
    free(p);
    return 0;
}

void cadastroProdutos(struct produtos **p, int tamanho){

    int i = 0;
    for (i = 0; i < tamanho; i++) {

        printf("Insira o ID: ");
        scanf("%d", &(p[i]->id));
        printf("Insira o nome: ");
        scanf("%s",  p[i]->nm);
        printf("Insira o valor: ");
        scanf("%f", &(p[i]->vlrs));
        printf("Insira a quantidade: ");
        scanf("%d", &(p[i]->quant));

    }
}

void maiorValorProdutos(struct produtos **p, int tamanho){



}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM