1

I need help with the following problem:

Given an array arr of structs

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

Print the longest subarray of elements from array arr such that the arr element has greater or equal price than some value which is read.

Function to check if element has greater or equal price than a value is given as an argument of a function void subarray(product *arr,int n,int (*check)(product * ,float ), product *newArr,int *len_newArr,float value) where newArr is the output subarray.

Here is my code:

#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;
}

The program gives warnings assignment makes pointer from integer without a cast at line

    //begin with the next subarray
    current_len=1;

and comparison between pointer and integer at line

//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);
ufo
  • 93
  • 1
  • 11
  • Why do you declare `current_len` as a pointer? And what do you think initializing this pointer to `0` does? – Some programmer dude Sep 21 '16 at 09:29
  • I also don't see why you pass `newArr` and `len_newArr` as arguments to the function? They are not used outside the `subarray` function. And *if* you use `newArr` outside the function you need to pass a pointer to the pointer (i.e. `product **`) to the function. – Some programmer dude Sep 21 '16 at 09:31
  • Finally, you allocate memory for `newArr` in the `subarray` function, but you never initialize it before you print the contents of it. Lots and lots and lots of things that does (or could) lead to *undefined behavior*. – Some programmer dude Sep 21 '16 at 09:35

1 Answers1

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

This

        *current_len++;

Is equivalent to *NULL++ and you can not dereference a pointer to NULL. [More info]

Same here:

*current_len=1;

It seems that you want a plain int instead of a pointer to int

Community
  • 1
  • 1
David Ranieri
  • 39,972
  • 7
  • 52
  • 94