1

次の問題について助けが必要です。

与えられたarr配列structs

typedef struct
{
    char name[20];
    float amount,price;
}product;

要素が読み取られたものよりも大きいか等しいarrように、array から要素の最長の部分配列を出力します。arrpricevalue

price要素がa以上かどうかをチェックvalueする関数は、関数の引数として与えられます。void subarray(product *arr,int n,int (*check)(product * ,float ), product *newArr,int *len_newArr,float value) ここnewArrで、出力部分配列です。

これが私のコードです:

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

typedef struct
{
    char name[20];
    float amount,price;
}product;

void subarray(product *arr,int n,int (*check)(product * ,float ),
                product *newArr,int *len_newArr,float value)
{
    len_newArr=0;
    int *current_len;
    current_len=0;
    int i;

    for(i=0;i<n;i++)
    {
        //if condition is true, increment current length of newArr
        //and store that element to newArr
        if((*check)(arr+i,value))
        {
            current_len++;
            newArr[i]=arr[i];
        }
        else
            //begin with the next subarray
            current_len=1;

        //update newArr length
        if(current_len > len_newArr)
            len_newArr=current_len;
    }

    newArr=calloc(*len_newArr , sizeof(product));

    //print the subarray
    for(i=0;i<len_newArr;i++)
        printf("%-19s %6.2f %6.2f\n",newArr[i].name,newArr[i].amount,newArr[i].price);
}

int check(product *pr,float value)
{
    if(pr->price >= value)
        return 1;
    return 0;
}

void inputProduct(product *pr)
{
    printf("name: ");
    scanf("%s",pr->name);
    printf("amount: ");
    scanf("%f",&pr->amount);
    printf("price: ");
    scanf("%f",&pr->price);
}

int main()
{
    int n,i;
    product *arr,*newArr;
    int len_newArr;
    float value;

    do
    {
        printf("n = ");
        scanf("%d",&n);
    }
    while(n<1);

    arr=malloc(n * sizeof(product));
    newArr=calloc(n,sizeof(product));

    for(i=0;i<n;i++)
    {
        printf("%d. product: \n",i+1);
        inputProduct(arr+i);
    }

    printf("value: ");
    scanf("%f",&value);

    subarray(arr,n,&check,newArr,&len_newArr,value);

    return 0;
}

assignment makes pointer from integer without a castプログラムは次の行で警告を出します

    //begin with the next subarray
    current_len=1;

そしてcomparison between pointer and integer行で

//print the subarray
for(i=0;i<len_newArr;i++)
    printf("%-19s %6.2f %6.2f\n",newArr[i].name,newArr[i].amount,newArr[i].price);
4

1 に答える 1

1
int *current_len=0; /* assigining NULL to a pointer to int */

これ

        *current_len++;

は と同等で*NULL++あり、 へのポインターを逆参照することはできませんNULL。[詳細]

こっちも一緒:

*current_len=1;

intへのポインタではなくプレーンが必要なようですint

于 2016-09-21T09:31:20.123 に答える