【问题标题】:I need to pass an array of structures to a function [large code]我需要将结构数组传递给函数[大代码]
【发布时间】:2014-12-11 17:11:54
【问题描述】:

我已经为此工作了几天,但似乎无法成功。

提前抱歉,这篇文章太冗长了,所以如果有人花时间阅读它并试图理解这个烂摊子,我会欠你的。

代码如下:

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

typedef struct cart {
    int id;
    char *nume;
} cart;

typedef struct pach {
    int id, idCartier, strada, numar, prioritate, codificare;
    float greutate;
    char* mesaj;
    int adresa[18];
} pach;

typedef struct post {
    int id, nrPachete;
    int vector[50];
} post;

int citeste(int *nrP, cart *cartier, pach *pachet, int *nrC) {
    printf("Punctul 1\n");
    int i, j;     
    scanf("%d", nrC);
    for (i = 0; i < *nrC; i++) {
        cartier[i].id = i;
        char aux[500]; 
        scanf("%s", aux);
        cartier[i].nume = malloc(strlen(aux) + 1);  
        cartier[i].nume = aux;
        printf("%d %s\n", cartier[i].id, cartier[i].nume);
    }
    scanf("%d", nrP);
    for (i = 0; i < *nrP; i++) {
        pachet[i].id = i;
        char aux[500];
        for (j = 0; j < 18; j++)
            scanf("%d", &pachet[i].adresa[j]);
        scanf("%d %f", &pachet[i].prioritate, &pachet[i].greutate);
        getchar();
        fgets(aux, 256, stdin);
        pachet[i].mesaj = malloc(strlen(aux) + 1);
        pachet[i].mesaj = aux;
        printf("%d\n", pachet[i].id);
        for (j = 0; j < 18; j++)
            printf("%d ", pachet[i].adresa[j]);
        printf("\n%d %.6f ", pachet[i].prioritate, pachet[i].greutate); 
        printf("%s", pachet[i].mesaj);
    }
    return *nrP;
}

void extrage(int *nrP, pach *pachet) {
    printf("\nPunctul 2\n");
    int i, j;
    for (i = 0; i < *nrP; i++) {
        pachet[i].idCartier = 0;
        pachet[i].strada = 0;
        pachet[i].numar = 0;
        for (j = 0; j < 5; j++)
            pachet[i].idCartier += pachet[i].adresa[j] * pow(2, (4 - j));
        for (j = 5; j < 10; j++)
            pachet[i].strada += pachet[i].adresa[j] * pow(2, (9 - j));
        for (j = 10; j < 18; j++)
            pachet[i].numar += pachet[i].adresa[j] * pow(2, (17 - j));
        printf("%d %d ", pachet[i].id, pachet[i].idCartier);
        printf("%d %d\n", pachet[i].strada, pachet[i].numar);
    }
}

void distribuie(int *nrP, pach *pachet, post *postas, int *nrC, cart *cartier) {    
    printf("Punctul 3\n");
    int i, j;
    for (i = 0; i < *nrC; i++) {        // FOR-1A
        postas[i].nrPachete = 0;
        postas[i].id = i;
        for (j = 0; j < 50; j++)
            postas[i].vector[j] = 0;
    }
    for (i = 0; i < *nrC; i++) {        // FOR-1B
        for (j = 0; j < *nrP; j++) {        
            if (cartier[i].id == pachet[j].idCartier) {
                postas[i].vector[postas[i].nrPachete] = pachet[j].id;
                postas[i].nrPachete++;
            }
        }
        printf("%d %d ", postas[i].id, postas[i].nrPachete);
        for (j = 0; j < postas[i].nrPachete; j++)
            printf("%d ", postas[i].vector[j]);
        printf("\n");
    }


}


void ordoneaza(pach *pachet, int *nrC, post *postas) {
    printf("Punctul 4\n");
    pach aux;
    int i, j, k = 0, schimbat = 1;
    for (i = 0; i < *nrC; i++) {
        while (schimbat) {
            schimbat = 0;
            for (j = 0; j < postas[i].nrPachete - k; j++)
                if (pachet[postas[i].vector[j]].prioritate < pachet[postas[i].vector[j+1]].prioritate) {
                    aux = pachet[postas[i].vector[j]];
                    pachet[postas[i].vector[j]] = pachet[postas[i].vector[j+1]];
                    pachet[postas[i].vector[j+1]] = aux;
                    schimbat = 1;
                }
            k++;
        }
        k = 0;
        schimbat = 1;
        for (j = 0; j < postas[i].nrPachete; j++) {
            for (k = j; k < postas[i].nrPachete; k++) {
                if (pachet[postas[i].vector[j]].prioritate == pachet[postas[i].vector[k]].prioritate) 
                    if (pachet[postas[i].vector[j]].greutate < pachet[postas[i].vector[k]].greutate) {
                        aux = pachet[postas[i].vector[j]];
                        pachet[postas[i].vector[j]] = pachet[postas[i].vector[k]];
                        pachet[postas[i].vector[k]] = aux;
                    }
            }   
        }
    }
    for (i = 0; i < *nrC; i++)
        for (j = 0; j < postas[i].nrPachete; j++) {
            postas[i].vector[j] = pachet[postas[i].vector[j]].id;
            }
    for (i = 0; i < *nrC; i++) {
        printf("%d %d ", postas[i].id, postas[i].nrPachete);
        for (j = 0; j < postas[i].nrPachete; j++)
            printf("%d ", postas[i].vector[j]);
        printf("\n");
    }
}


int main() {
    int nrP, nrC;
    pach pachet[1600];
    post postas[32];
    cart cartier[32];
    citeste(&nrP, &cartier[32], &pachet[1600], &nrC);
    extrage(&nrP, &pachet[1600]);
    distribuie(&nrP, &pachet[1600], &postas[32], &nrC, &cartier[32]);
    ordoneaza(&pachet[1600], &nrC, &postas[32]);
    return (0);
}

关于程序功能的简短信息:

citeste 函数应该读取 cartier 和 pachet 结构。他们都是。然后以稍微不同的格式打印出来。

extrage 函数应该取每个 pachet,并使用 adresa(用 BINARY 编写)将其 3 部分转换并获得 strada、numar 和 idCartier。然后也打印那些。

Distribuie 检查 pachet 是否分发到 postas(distributed 意味着 pachet.idCartier == postas.id),如果没有则分发它。

Ordoneaza 获取每个 postas 的向量并在优先级之后对其进行排序(如果优先级相等,则使用 greutate)。

但它不能按预期工作,并且还会产生奇怪的分段错误。

例如,如果我注释掉 distribuie 函数,它会在 extrage 后立即给我 segfault。如果我把它放回去,它会在完成后立即给出段错误。如果我取消注释所有内容,最后会再次出现段错误。

如果有人真正阅读了所有这些内容并愿意回复,我将不胜感激。任何建议都有帮助!

【问题讨论】:

  • 首先你不要像那样传递数组。第一个函数调用应该是 citeste(&nrP, cartier, pachet, &nrc)。
  • 你正在做的是传递一个甚至不存在的元素的地址,因为数组索引应该从 0 运行到 size-1。
  • citeste(&amp;nrP, &amp;cartier[32], &amp;pachet[1600], &amp;nrC); --> citeste(&amp;nrP, cartier, pachet, &amp;nrC);,例如&amp;cartier[32] 表示最后一个元素的后续元素的地址。
  • 你不能像cartier[i].nume = aux那样复制字符串,请改用strcpy/strncpy/strdup
  • 要问另一个问题,请使用“问问题”按钮。它似乎与这个完全无关。

标签: c arrays function vector structure


【解决方案1】:

我没有阅读您的代码,但您的标题说您在传递结构数组时遇到了问题。我附上了一个有效的 sn-p 希望它能帮助您解决问题。

#include<stdio.h>
typedef struct employee{
int empId;
char name[10];
}EMP;

void arrayOfStruct(EMP *a, int size)
{
    printf("%d\t%d",a[0].empId,a[3].empId);    
}
int main()
{
   EMP NC[4];
   NC[0].empId = 9;
   NC[3].empId = 2;
   arrayOfStruct(&NC[0],sizeof(NC)/sizeof(NC[0]));
}

size 的帮助下,您永远不会超出为结构分配的内存。

如果你想传递更高维的数组,你必须硬编码除了最外面的数组的所有大小。

void arrayOfStruct(EMP a[][4], int size)
{
    // to do
}

int main()
{
    EMP NC[2][4];
    ...
    arrayOfStruct(NC,sizeof(NC)/sizeof(NC[0]));
}

如你所见,我没有指定更大的数组大小,我通过其他参数传递。

为什么需要指定内部尺寸的大小?

举个例子,假设你有int[4][4],并且你试图通过int[3][]将数组传递给一个函数,编译器如何知道要创建多少个内部块,在其他情况下通过int[][3] ,编译器可以很容易地理解它必须为每个外部数组制作大小为 3 的内部块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多