简体   繁体   English

另一个function中带有数组的双指针

[英]Double pointer with array in another function

I have to create a program that has an array of costumers (structs that contain name, code and documentation) and functions to insert, remove and list all of them in order of code.我必须创建一个程序,该程序具有一组客户(包含名称、代码和文档的结构)以及按代码顺序插入、删除和列出所有这些的函数。 I'm not understanding what I should do.我不明白我应该做什么。 Please note that the parameters for insertCostumer , removeCostumer and listCostumer cannot be changed.请注意insertCostumerremoveCostumerlistCostumer的参数不能更改。

Piece of code 01:代码01:

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

#define MAX_REG 10

typedef struct _costumer {
    int code;
    char name[50];
    char documentation[20];
} costumer;

Piece of code 02:代码02:

int main(int argc, char** argv) {
    costumer *costumers[MAX_REG];
    costumer **p_costumer;
    p_costumer = &costumers[0];
    int count = 0;

    memset(costumers, 0, sizeof(costumers));

    //Some code to check what to do using a switch
            case '1': insertCostumer(p_costumer, &count); getch(); break;
            case '2': removeCostumer(p_costumer, &count); getch(); break;
            case '3': listCostumers(p_costumer, &count); getch(); break;
    //Some code
    return (EXIT_SUCCESS);
}

Piece of code 03:代码03:

void insertCostumer(costumer **p_costumer, int *count){
    char aux[50];
    char aux2[20];

    if(*count < MAX_REG) {
      *p_costumer = (costumer *) malloc(sizeof(costumer));
      printf("\nInsert the code: ");
      gets(aux);
      (*p_costumer)->code = atoi(aux);

      printf("Insert the name: ");
      gets(aux);
      strcpy((*p_costumer)->name, aux);

      printf("Insert the documentation: ");
      gets(aux2);
      strcpy((*p_costumer)->documentation, aux2);

      (*count)++;
      p_costumer = &*p_costumer[*count];

    } else {
        printf("List full! Remove a costumer first!\n");
    }
}

void removeCostumer(costumer **p_costumer, int *count){
    char aux3[50];
    int cod;

    printf("\nInsert the code of the costumer to be removed: ");
    gets(aux3);
    cod = atoi(aux3);

    for(int i = 0; i < *count; i++) {
        if(p_costumer[i]->code == cod) {
            strcpy(p_costumer[i]->name, NULL);
            p_costumer[i]->code = 0;
            strcpy(p_costumer[i]->documentation, NULL);
        }
      }
}

void listCostumers(costumer **p_costumer, int *count){
    for(int i = 0; i < *count; i++) {
        printf("Code: %d | Name: %s | Documentation: %s\n", p_costumer[i]->code, p_costumer[i]->name, p_costumer[i]->documentation);
    }
}

I don't know what I'm doing wrong;我不知道我做错了什么; nothing is working, honestly.老实说,没有任何工作。 I was trying to first insert, list and remove to try and make the sorting part later, but I can't even get this part done.我试图先插入、列出和删除,然后再尝试进行排序部分,但我什至无法完成这部分。 When I list, only the last costumer added is listed, for example.例如,当我列出时,仅列出最后添加的客户。

Can someone help me?有人能帮我吗?

Okay, I had to refactor a considerable amount of your code, so I don't have a blow by blow description of the changes.好的,我不得不重构你的大量代码,所以我没有对这些变化的逐个描述。

You'll just have to study it a bit.你只需要研究一下。

Note that even if you're passed a double pointer as an argument, doesn't mean you have to use it as a double in the body of the functions.请注意,即使您将双精度指针作为参数传递,也不意味着您必须将其用作函数体中的双精度。 Note, in particular, what I did for the count (eg int count = *p_count; and *p_count = count; )请特别注意我为计数所做的事情(例如int count = *p_count;*p_count = count;

But, it should be noted that the list is one of pointers to structs and not merely a pointer to an array of structs (ie there is an extra level of indirection).但是,应该注意,列表是指向结构的指针之一,而不仅仅是指向结构数组的指针(即存在额外的间接级别)。 This makes things a bit faster.这使事情变得更快。

Note that, bug fixes aside, the key is the "slide" operation in the remove function.请注意,除了错误修复之外,关键是删除 function 中的“滑动”操作。

Because we're "sliding" pointers, this is faster/more efficient with the pointer array.因为我们正在“滑动”指针,所以使用指针数组更快/更有效。 Study this [concept] well.好好研究这个[概念]。

Never use gets --always use fgets从不使用gets总是使用fgets

I've deliberately left off comments.我故意留下评论。 This will allow you to add them as you analyze the code.这将允许您在分析代码时添加它们。 I've found that this can be a powerful technique for understanding a [foreign] code base.我发现这可以成为理解[外国]代码库的强大技术。

Anyway, here's the code.无论如何,这是代码。 I've done some rudimentary testing and it seems to work:我做了一些基本的测试,它似乎工作:

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

#define MAX_REG 10
char aux[1000];

typedef struct _costumer {
    int code;
    char name[50];
    char documentation[20];
} costumer;

void
lineget(char *buf,size_t buflen)
{
    char *cp;

    cp = fgets(buf,buflen,stdin);

    if (cp != NULL) {
        cp = strrchr(buf,'\n');
        if (cp != NULL)
            *cp = 0;
    }
}

void
insertCostumer(costumer **p_costumer, int *p_count)
{
    costumer *add;
    int count = *p_count;

    char aux2[20];

    if (count < MAX_REG) {
        add = malloc(sizeof(costumer));

        printf("\nInsert the code: ");
        lineget(aux,sizeof(aux));
        add->code = atoi(aux);

        printf("Insert the name: ");
        lineget(add->name,sizeof(add->name));

        printf("Insert the documentation: ");
        lineget(add->documentation,sizeof(add->documentation));

        p_costumer[count] = add;
        ++count;
    }
    else {
        printf("List full! Remove a costumer first!\n");
    }

    *p_count = count;
}

void
removeCostumer(costumer **p_costumer, int *p_count)
{
    int count = *p_count;
    int cod;
    int i;
    costumer *cur;

    printf("\nInsert the code of the costumer to be removed: ");
    fgets(aux,sizeof(aux),stdin);
    cod = atoi(aux);

    int slide = 0;
    for (i = 0; i < count; i++) {
        cur = p_costumer[i];
        if (cur->code == cod) {
            slide = 1;
            break;
        }
    }

    if (slide) {
        free(cur);
        --count;

        for (;  i < count;  ++i)
            p_costumer[i] = p_costumer[i + 1];

        p_costumer[count] = NULL;
    }

    *p_count = count;
}

void
listCostumers(costumer **p_costumer, int *p_count)
{
    costumer *cur;
    int count = *p_count;

    for (int i = 0; i < count; ++i, ++cur) {
        cur = p_costumer[i];
        printf("Code: %d | Name: %s | Documentation: %s\n",
            cur->code, cur->name, cur->documentation);
    }
}

int
main(int argc, char **argv)
{
    costumer *costumers[MAX_REG];
    costumer **p_costumer;
    char buf[100];

    p_costumer = &costumers[0];
    int count = 0;

    memset(costumers, 0, sizeof(costumers));
    setbuf(stdout,NULL);

    //Some code to check what to do using a switch
    while (1) {
        printf("operation to perform (1=insert, 2=remove, 3=print): ");
        char *cp = fgets(buf,sizeof(buf),stdin);
        if (cp == NULL)
            break;

        switch (cp[0]) {
        case '1':
            insertCostumer(p_costumer, &count);
            break;

        case '2':
            removeCostumer(p_costumer, &count);
            break;

        case '3':
            listCostumers(p_costumer, &count);
            break;
        }
    }

    return (EXIT_SUCCESS);
}

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

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