简体   繁体   中英

how we can define and use structure inside function in C?

I am trying to create a structure inside a function and i am trying to use it. But when I am comparing two values from structure I am getting this message :

"C:\\Users\\hp\\Desktop\\prgm\\cap.c||In function 'fnumber':| C:\\Users\\hp\\Desktop\\prgm\\cap.c|

72|error: request for member 'f_value' in something not a structure or union| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

this is my code:-

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

/*
 * Complete the function below.
*/
int fnumber(long input1,int input2_size, int* input2)
{
    int i, j, k, count[input2_size],temp=0;
    int arr_temp[input2_size];
    struct smaller{
            int position;
            int f_value;};

     struct smaller list[input2_size];
     struct smaller temp1;
    memccpy(arr_temp, input2, input2_size,sizeof(int));
    for(i=0; i<input2_size;i++)
    {
        for(j=i+1; j<=input2_size;i++)
        {
            if(arr_temp[i]>arr_temp[j])
            {
                temp=arr_temp[i];
                arr_temp[i]=arr_temp[j];
                arr_temp[j]=temp;
            }
        }
    }
    for(i=0; i<input2_size;i++)
    {
        if(i>1)
        count[i]=input2[i];
        for(j=0;j<input2[i-1];j++)
        {
            count[i]+=arr_temp[j];
        }
    }
   /* for(i=0;i<input2_size-1;i++)
    {
        for(j=i+1;j<input2_size;j++)
        {
            if(count[i]>count[j])
            {
            temp=count[i];
            count[i]=count[j];
            count[j] = temp;
            }
        }

    }*/
    for(i=0,j=0;i<input2_size;i++)
    {
        if(count[i]<=input1)
            continue;
        else
        {
            list[j].position=i;
            list[j].f_value=count[i];
            j++;
        }

    }
    for(i=0;i<input2_size-1;i++)
    {
        for(j=i+1;j<input2_size;j++)
        {
            if(list[i].f_value>list[j.f_value])
            {
                temp1=list[i];
                list[i]=list[j];
                list[j]=temp1;
            }
        }
    }
    return list[0].position;

}

int main() {
int output = 0;
long ip1;
scanf("%ld", &ip1);
int ip2_size = 0;
int ip2_i;
scanf("%d\n", &ip2_size);
int ip2[ip2_size];
for(ip2_i = 0; ip2_i < ip2_size; ip2_i++) {
    int ip2_item;
    scanf("%d", &ip2_item);

    ip2[ip2_i] = ip2_item;
}
output = fnumber(ip1,ip2_size,ip2);
printf("%d\n", output);
return 0;
}
        if(list[i].f_value>list[j.f_value])

You misspelled list[j].f_value as list[j.f_value] .

Also, memccpy should be memcpy .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM