格式'%lf'需要'double *'类型的参数,但参数2的类型为'polyTerm **

时间:2017-11-21 16:31:58

标签: c++ c

这是头文件:

@media screen and (-webkit-min-device-pixel-ratio:0) {
/* rules only apply in saf3+, chrome1+ */
}

这是主文件:

#include <stdlib.h>

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

//////////////////////////////////////
// define structure for polynomial's
//////////////////////////////////////
typedef struct {
  double coeff; //varible for coeff type
} polyTerm;     //poly expoent will be array index value

/////////////////////////////////
// Define a polynomial
/////////////////////////////////
typedef struct {
  int used;           // slots used so far
  int capacity;       // total available slots
  polyTerm *data;     // array of type polyTerm we're storing
} polynomial;


polynomial* createPoly (int capacity);

#endif

这是创建多项式的函数:

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

int select = -1;
int maxExpo;//max expoent of polynoimal also max array length of which index is
        //expoent and is passed to the funtion as below.

int main()
{
  while (select != 0)
    {
      //user menu to select operations
      printf("\t(0) Exit program\n");
      printf("\t(1) Create new polynomial\n");
      printf("\t(2) Delete polynomial\n");
      printf("\t(3) Add polynomials\n");
      printf("\t(4) Subtract polynomials\n");
      printf("\t(5) Multiply polynomials\n");
      printf("\t(6) Divide polynomials\n");
      printf("\t(7) Normalise polynomials\n");
      printf("\t(8) Return order of polynomial\n");
      printf("\t(9) Print polynomial\n");
      printf("\nPlease select any operation 0-9:");
      scanf("%d",&select);

      switch (select)
    {
    case 0: //Exit program
      break;
    case 1:  //create a poly
        printf("\tPlease enter highest polynomial degree value of poly 1: ");
        scanf("%i",&maxExpo); 
        polynomial *p1 = createPoly(maxExpo); //polynomial array number 1
            for (int i=0; i<=maxExpo; i++)
      {
        printf("\tPlease enter coeff of x^%i :",i);
        scanf("%lf",&(p1[i].data));
      }
        //polynomial *p2 = createPoly(n); //polynomial array number 2
        // polynomial *result = createPoly(size of max degree of both poly's);
        //polynomial array number 3 which is the math function result array.


      break;
    case 2: //Delete poly

      break;
    case 3: //Add poly

      break;
    case 4: //Subtract poly

      break;
    case 5: //Mulyiply poly

      break;
        case 6: //divide poly

      break;
    case 7: //Normalise poly

      break;
    case 8: //Return order of poly

      break;
    case 9: //Print poly

      break;
    default : //invalid selection
      printf("\tInvalid selection please select one of the above operations 0-9\n\n");
    }
    }
  return EXIT_SUCCESS;
}

编译时出现以下错误

 polynomial* createPoly (int capacity){

  polynomial *inPoly;
  inPoly = (polynomial *) malloc(sizeof(polynomial));

  if (inPoly != NULL)
    {
    //int number = capacity * sizeof(polynomial);
      inPoly->data = (polyTerm *) calloc(capacity , sizeof(polyTerm));

      if (inPoly->data != NULL)  {
    // do something (allocation was successful)
    //inPoly->data = NULL;
    inPoly->capacity = capacity;
    inPoly->used = 0;
      } else {
    // allocation failed, clean up
    free(inPoly);
    inPoly = NULL;
    }

    }


    return inPoly;
}

我只是c代码的新手,上面的内容我不明白我正在经历艰难的时光。任何帮助将不胜感激。

感谢

1 个答案:

答案 0 :(得分:4)

问题在于:

  polynomial *p1 = createPoly(maxExpo);
  for (int i=0; i<=maxExpo; i++)
  {
    printf("\tPlease enter coeff of x^%i :",i);
    scanf("%lf",&(p1[i].data));
  }

字段p1[i].datapolyTerm *(其地址属于polyTerm **类型)。 scanf期待double的地址。

createPoly函数仅返回指向单个polynomial的指针,因此p1[i]没有意义,因为它意味着polynomial的数组。由于data成员指向polyTerm数组,并且每个成员都包含double类型的字段,这可能是您要写入的内容:

scanf("%lf",&(p1->data[i].coeff));