在C中通过地址传递struct-within-pointer-array-within-struct

时间:2011-03-08 02:49:40

标签: c pointers struct

我有一个函数,它将指针,多项式作为一个名为POLYNOMIAL的结构的实例。它包含一个指向另一个结构的指针数组,称为TERM。 TERM包含两个整数。

此函数用于修改TERM中的一个整数。我试着这样做:

polynomial->terms[i]->Coefficient *= scalar;

但这不起作用;当我打电话给

时,功能会看到我的期望

polynomial->terms[i]->Coefficient

这些变化显然仅限于该功能的范围。我已经想出了我对记忆的最好理解,试图找到问题,但没有用。

编辑:多项式定义:

typedef struct Polynomial {
  TERM *terms[MAXPOLYTERMS];
  int numTerms;
  char Polynomialstring[MAXPOLYCHARS];
} POLYNOMIAL;

术语定义:

typedef struct Term {
  int Coefficient;
  int Exponent;
  char Termstring[MAXTERMCHARS];
} TERM;`

功能:

void MultiplyPolynomial(POLYNOMIAL *polynomial, int scalar)
{

    int numberofterms = polynomial->numTerms, i=0;

    if(scalar == 0)
            {FreePolynomial(polynomial);}
    else
    {
            for(i=0;i<numberofterms;i++)
                    {polynomial->terms[i]->Coefficient *= scalar;}
    }

    return;
}

1 个答案:

答案 0 :(得分:2)

您向我们展示的代码没有任何明显错误,因此问题可能出在您未向我们展示的代码中。这是一个相当复杂的数据结构,需要管理;我想知道你加载它的代码是否有错误。也许您应该向我们展示初始化和分配数据的代码,以及用于打印数据的代码 - 以及测试程序。它可能只是一个二次(3项)多项式;那就足够了。

我发现麻烦在于你在多项式中分配terms,我并不感到惊讶。你可能真的想要:

typedef struct Polynomial
{
    TERM *terms;
    int numTerms;
    char Polynomialstring[MAXPOLYCHARS];
} POLYNOMIAL;

然后,您可以使用以下内容分配正确数量的术语:

poly->terms = malloc(nterms * sizeof(*poly->terms));
poly->nterms = nterms;

您可以使用与以前几乎相同的表达式来访问术语:

poly->terms[i].Coefficient *= scalar;

请注意最后一部分从->.的更改。

为简洁明了,省略了错误检查。

您可能不会按照现有数据结构的要求对MAXPOLYTERMS进行单独分配的分配。


使用简化多项式结构的版本

我已经删除了代码中未使用的部分(比如字符串)以获得此版本,该版本在MacOS X 10.6.6下在GCC 4.2.1(使用-std=c99 -Wall -Wextra)下编译清理并在Valgrind下运行清理3.6.1。

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

typedef struct Term
{
    int Coefficient;
    int Exponent;
} TERM;

typedef struct Polynomial
{
    TERM *terms;
    int numTerms;
} POLYNOMIAL;

static void FreePolynomial(POLYNOMIAL *poly)
{
    free(poly->terms);
    free(poly);
}

static void MultiplyPolynomial(POLYNOMIAL *polynomial, int scalar)
{
    int numberofterms = polynomial->numTerms;

    for (int i = 0; i < numberofterms; i++)
        polynomial->terms[i].Coefficient *= scalar;
}

static POLYNOMIAL *allocPoly(int *array, int num)
{
    POLYNOMIAL *poly = malloc(sizeof(*poly));
    poly->terms = malloc(num * sizeof(*poly->terms));
    for (int i = 0; i < num; i++)
    {
        poly->terms[i].Coefficient   = array[2*i+0];
        poly->terms[i].Exponent      = array[2*i+1];
    }
    poly->numTerms = num;
    return poly;
}

static void dumpPolynomial(FILE *fp, POLYNOMIAL *poly, const char *tag)
{
    fprintf(fp, "Polynomial dump (%s): 0x%p\n", tag, poly);
    if (poly != 0)
    {
        fprintf(fp, "Terms %d\n", poly->numTerms);
        for (int i = 0; i < poly->numTerms; i++)
            fprintf(fp, "%d: Coefficient %d, Exponent %d\n", i,
                poly->terms[i].Coefficient, poly->terms[i].Exponent);
    }
    fflush(fp);
}

int main(void)
{
    int coeffs[] = { 1, 0, 2, 1, 3, 2 };
    POLYNOMIAL *poly = allocPoly(coeffs, 3);
    dumpPolynomial(stdout, poly, "Before");
    MultiplyPolynomial(poly, 4);
    dumpPolynomial(stdout, poly, "After");
    FreePolynomial(poly);
    return 0;
}

使用复杂多项式结构的版本

这是使用原始数据结构。它编译清洁并运行清洁。

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

enum { MAXPOLYTERMS = 5, MAXPOLYCHARS = 5, MAXTERMCHARS = 5 };

typedef struct Term {
  int Coefficient;
  int Exponent;
  char Termstring[MAXTERMCHARS];
} TERM;

typedef struct Polynomial {
  TERM *terms[MAXPOLYTERMS];
  int numTerms;
  char Polynomialstring[MAXPOLYCHARS];
} POLYNOMIAL;

static void FreePolynomial(POLYNOMIAL *poly)
{
    for (int i = 0; i < poly->numTerms; i++)
        free(poly->terms[i]);
    free(poly);
}

static void MultiplyPolynomial(POLYNOMIAL *polynomial, int scalar)
{

    int numberofterms = polynomial->numTerms, i=0;

    if(scalar == 0)
            {FreePolynomial(polynomial);}
    else
    {
            for(i=0;i<numberofterms;i++)
                    {polynomial->terms[i]->Coefficient *= scalar;}
    }

    return;
}

static POLYNOMIAL *allocPoly(int *array, int num)
{
    POLYNOMIAL *poly = malloc(sizeof(*poly));
    for (int i = 0; i < num; i++)
    {
        poly->terms[i] = malloc(sizeof(TERM));  // backsliding
        poly->terms[i]->Coefficient   = array[2*i+0];
        poly->terms[i]->Exponent      = array[2*i+1];
        poly->terms[i]->Termstring[0] = '\0';
    }
    poly->numTerms = num;
    poly->Polynomialstring[0] = '\0';
    return poly;
}

static void dumpPolynomial(FILE *fp, POLYNOMIAL *poly, const char *tag)
{
    fprintf(fp, "Polynomial dump (%s): 0x%p\n", tag, poly);
    if (poly != 0)
    {
        fprintf(fp, "Terms %d\n", poly->numTerms);
        for (int i = 0; i < poly->numTerms; i++)
            fprintf(fp, "%d: Coefficient %d, Exponent %d\n", i,
                poly->terms[i]->Coefficient, poly->terms[i]->Exponent);
    }
    fflush(fp);
}

int main(void)
{
    int coeffs[] = { 1, 0, 2, 1, 3, 2 };
    POLYNOMIAL *poly = allocPoly(coeffs, 3);
    dumpPolynomial(stdout, poly, "Before");
    MultiplyPolynomial(poly, 4);
    dumpPolynomial(stdout, poly, "After");
    return 0;
}