【问题标题】:Array of structures lose data while passing it in C结构数组在 C 中传递数据时会丢失数据
【发布时间】:2021-04-15 18:51:05
【问题描述】:

我不明白为什么 printq() 函数会打印 0,但是当我在 main() 中访问它时,它会打印。我不明白我做错了什么,我尝试使用指针,但在优先级队列中出现了一些不同的错误。我想打印数组pq[10]中的元素。

编辑:我意识到元素已存储,但是当我使用 pq[R].data 时,它会打印 但是当我在 printq() 中使用 pq[i].data 并将其放入 for 循环中时,它打印为零。

#include <stdio.h>

int F = -1, R = -1;
int item, max = 10;

struct prioq {
    int data;
    int prio;
};

struct prioq pq[10] = { 0 };

void printq()
{
    int i = 0;
    printf("%d,", pq[i].data);
    printf("QUEUE :");
    for (i = 0; i < max; i++) {
        printf("%d,", pq[i].data);
    }
    printf("\n");
    printf("PRIO  :");
    for (i = 0; i < max; i++) {
        printf("%d,", pq[i].prio);
    }
}

void enqueue(int item, int p, struct prioq pq[])
{
    if (F == -1 && R == -1 || F > R) {
        F == 0;
        R == 0;
        pq[R].data = item;
        pq[R].prio = p;

        printf("%d", pq[R].data);
        printf("%d", pq[R].prio);
        printq();

    } else if (R == max-1 || R > max) {
        printf("overflow\n");
    } else if (R < max) {
        R++;
        pq[R].data = item;
        pq[R].prio = p;
        printq();
    }
}

void dequeue(struct prioq pq[])
{
    int large = 0;
    if (F == -1) {
        printf("underflow\n");
    } else {
        int i;
        for (i = 0; i < max; i++) {
            if (pq[i].prio > large) {
                large = i;
            }
        }
    }

    item = pq[large].data;
    pq[large].prio = 0;
    pq[large].data = 0;

    printf("item deleted: %d\n", item);

    printq();
}

void main()
{
    int item = 0;
    int c = 0, p = 0;

    do {
        printf("choose your option\n");
        printf("1.Insert, 2.Delete, 3.Exit\n" );
        scanf("%d", &c);
        switch (c) {
            case 1:
                printf("Enter the priority and element to insert\n");
                scanf("%d %d", &item, &p);
                enqueue(item,p,pq);
                printf("%d", pq[R].data);
                printf("%d", pq[R].prio);
                break;

            case 2:
                dequeue(pq);
                break;

            default:
                c = 3;
                break;
        }
    } while (c != 3);
    printf("exited\n");
}

【问题讨论】:

  • C++ 不是 C,它是一种完全不同的语言。请不要将 C++ 标签用于 C 代码。
  • srry 我是 stackoverflow 的新手,我只是使用了推荐的标签
  • 您是否尝试过在调试器中逐行运行代码,同时监控所有变量的值,以确定您的程序在哪个点停止按预期运行?如果您没有尝试过,那么您可能想阅读以下内容:What is a debugger and how can it help me diagnose problems? 您可能还想阅读以下内容:How to debug small programs?
  • 即使使用调试器并不能真正解决问题,但它至少应该可以帮助您进一步隔离问题并创建minimal reproducible example,以便其他人更容易提供帮助你。大多数人不会愿意为你调试整个程序,因为那应该是你的工作。
  • 我只是环顾四周,发现“=”错误应该是“==”,谢谢您的时间。

标签: arrays c function structure


【解决方案1】:

在您的enqueue 函数中,将FR 分配中的== 更改为=

void enqueue(int item, int p, struct prioq pq[])
{
    if (F == -1 && R == -1 || F > R) {
        F = 0; // Here
        R = 0; // And here
        pq[R].data = item;
        pq[R].prio = p;

        printf("%d", pq[R].data);
        printf("%d", pq[R].prio);
        printq();

    } else if (R == max-1 || R > max) {
        printf("overflow\n");
    } else if (R < max) {
        R++;
        pq[R].data = item;
        pq[R].prio = p;
        printq();
    }
}

【讨论】:

  • 非常感谢,我明白了:)
猜你喜欢
  • 1970-01-01
  • 2017-06-12
  • 2020-08-03
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 2018-01-06
  • 2016-05-31
  • 2022-01-20
相关资源
最近更新 更多