【问题标题】:C - Longest subarray of specified elements from given arrayC - 给定数组中指定元素的最长子数组
【发布时间】:2017-01-29 11:17:38
【问题描述】:

我需要帮助解决以下问题:

给定一个数组 arrstructs

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

打印数组arr 中元素的最长子数组,使得arr 元素的price 大于或等于读取的某些value

检查元素是否具有大于或等于price 的函数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);

【问题讨论】:

  • 为什么将current_len声明为指针?您认为将这个指针初始化为0 会做什么?
  • 我也不明白你为什么将newArrlen_newArr 作为参数传递给函数?它们不在subarray 函数之外使用。 如果你在函数外使用newArr,你需要将一个指向指针的指针(即product **)传递给函数。
  • 最后,您在subarray 函数中为newArr 分配内存,但在打印它的内容之前从未对其进行初始化。很多很多的事情确实(或可能)导致未定义的行为

标签: c function-pointers sub-array


【解决方案1】:
int *current_len=0; /* assigining NULL to a pointer to int */

这个

        *current_len++;

等效于*NULL++,您不能取消引用指向NULL 的指针。 [More info]

这里也一样:

*current_len=1;

您似乎想要一个普通的int 而不是指向int 的指针

【讨论】:

    猜你喜欢
    • 2017-06-19
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 2020-06-21
    • 2014-01-16
    • 1970-01-01
    • 2011-10-27
    • 2012-01-21
    相关资源
    最近更新 更多