是什么导致C中的格式说明符错误?

时间:2015-11-10 19:54:18

标签: c

#include <stdio.h>


#define MAXPOINTS 10000

// compute the average of the first num_items of buffer
double avg(double buffer[], int num_items);

//update the max and min of the first num_items of array
void maxmin(double array[], int num_items, double* max, double* min);

//shift length-1 elements of the buffer to the left and put the 
//new_item on the right.
void updatebuffer(double buffer[], int length, double new_item);



int main(int argc, char* argv[]) 
{

    /* DO NOT CHANGE THIS PART OF THE CODE */
    double x[MAXPOINTS], y[MAXPOINTS], z[MAXPOINTS],num_z,num_y,num_x,avg_x,avg_y,avg_z,max_x,max_y,max_z,min_x,min_y,min_z;
    int lengthofavg = 0;
    if (argc>1) {
        sscanf(argv[1], "%d", &lengthofavg );
        printf("You entered a buffer length of %d\n", lengthofavg);
    }
    else {
        printf("Enter a length on the command line\n");
        return -1;
    }
    if (lengthofavg <1 || lengthofavg >MAXPOINTS) {
        printf("Invalid length\n");
        return -1;
    }
    /* PUT YOUR CODE HERE */
    double max;
    double min;
    int b_up, b_down,b_left,b_right,time;
    double ax,ay,az;

    int i; 

    for(i=0;i<lengthofavg;i++)

    {
        scanf("%d, %lf, %lf, %lf, %d, %d, %d, %d", &time, &a_x, &a_y, &a_z, &Button_UP, &Button_DOWN, &Button_LEFT, &Button_RIGHT);
        x[i] = num_x; 
        y[i] = num_y; 
        z[i] = num_z; 
    }


    while(1)
    {

        scanf("%d, %lf, %lf, %lf, %d, %d, %d, %d", &time, &a_x, &a_y, &a_z, &Button_UP, &Button_DOWN, &Button_LEFT, &Button_RIGHT);
        updatebuffer(x, lengthofavg, num_x);
        updatebuffer(y, lengthofavg, num_y);
        updatebuffer(z, lengthofavg, num_z);

        avg_x= avg(x, lengthofavg);
        avg_y = avg(y, lengthofavg);
        avg_z = avg(z, lengthofavg);

        maxmin(x, lengthofavg, &max_x, &min_x);
        maxmin(y, lengthofavg, &max_y, &min_y);
        maxmin(z, lengthofavg, &max_z, &min_z);

        printf("%lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf, %lf\n", num_z, num_y, num_x, avg_x, avg_y, avg_z, max_x, max_y, max_z, min_x, min_y, min_z);

        fflush(stdout);

        if(Button_LEFT == 1)

        {

            return 0;

        }
    }
}   

double avg(double buffer[], int num_items)
{
    double average = 0;
    int i = 0;
    for(i=0;i<num_items;i++)
    {
        average = buffer[i] + average;
    }

    average = average/num_items;
    return average;
}

void maxmin(double array[], int num_items, double* max, double* min)
{
    int i = 0;
    *max = array[0];
    *min = array[0];
    for(i=0;i<num_items;i++)
    {
        if(array[i] > *max)
        {
            *max = array[i];
        }

        else if(array[i] < *min)
        {
            *min = array[i];
        }

    }
}

void updatebuffer(double buffer[], int length, double new_item)
{
    int i = 0;
    for(i=0;i < length;i++)
    {
        buffer[i] = buffer[i+1];
    }
    buffer[length-1] = new_item;
}

我的两个“scanf()”语句都给出了格式说明符警告和未声明标识符的错误。

  
      
  • 警告:format指定类型'int *'但参数具有类型   '&lt; dependent type&gt;'
  •   
  • 错误:使用未声明的标识符
  •   

是什么导致这种情况发生?

2 个答案:

答案 0 :(得分:0)

您声明的变量与传递给scanf函数的变量不匹配:

 int b_up, b_down,b_left,b_right,time;
 double ax,ay,az;

 scanf("%d, %lf, %lf, %lf, %d, %d, %d, %d", &time, &a_x, &a_y, &a_z, &Button_UP, &Button_DOWN, &Button_LEFT, &Button_RIGHT);

像这样改变它们:

 scanf("%d, %lf, %lf, %lf, %d, %d, %d, %d", &time, &ax, &ay, &az, &b_up, &b_down, &b_left, &b_right);

答案 1 :(得分:0)

scanf("%d, %lf, %lf, %lf, %d, %d, %d, %d", &time, &a_x, &a_y, &a_z, &Button_UP, &Button_DOWN, &Button_LEFT, &Button_RIGHT);

在这一行中,所有按钮都是在没有声明的情况下传递的。

int b_up, b_down,b_left,b_right,time;

b_up!= Button_UP 或者相反。但不是两个。

尝试: scanf("%d, %lf, %lf, %lf, %d, %d, %d, %d", &time, &a_x, &a_y, &a_z, &b_up, &b_down, &b_left, &b_right);

另外,考虑使用gdb或 valgrind ,在开发过程中会告诉你更多。不仅是你声明和未使用的内容,还包括你对堆和内存的粗心大意。

快乐的编码!。